Welcome Guest, Not a member yet? Register   Sign In
calling base_url() or site_url() in the external js file
#1

[eluser]jairoh_[/eluser]
Good day everyone.

Most of my script was inline because of the fact i'm using paths dynamically using base_url or site_url,
but for the reason i want to separate it in another file, but how am i gonna use base_url or site_url on another js file? tnx
#2

[eluser]anis2505[/eluser]
Hi, I don't think that's possible. the best way -as I think- you just create a js script and create these two function and include this script at the top of you web pages

Code:
JS File

var baseUrl = "you/base/url";
var siteUrl ="your/site/url";
var index="yourindex.php";

function base_url(uri){
return baseUrl+uri;
}

function site_url(uri){
  return siteUrl+index+"/"+uri;
}
#3

[eluser]jairoh_[/eluser]
[quote author="anis2505" date="1372808560"]Hi, I don't think that's possible. the best way -as I think- you just create a js script and create these two function and include this script at the top of you web pages

Code:
JS File

var baseUrl = "you/base/url";
var siteUrl ="your/site/url";
var index="yourindex.php";

function base_url(uri){
return baseUrl+uri;
}

function site_url(uri){
  return siteUrl+index+"/"+uri;
}
[/quote]

yeah. tsk. anyway thankyou sir.
#4

[eluser]Syllean[/eluser]
Pass it to your js function as a parameter
#5

[eluser]CroNiX[/eluser]
Sure you can, just use php to output the js vars in the head of your document before loading your external js scripts.

In your template, in the head section:
Code:
<head>
<skript type="text/javascript">
&lt;?php echo $js_vars; ?&gt;
</skript>
&lt;?php // load your other js files below this ?&gt;

&lt;/head&gt;

In your controller (or better yet, MY_Controller):
Code:
$data['js_vars'] = 'var base_url = "&lt;?php echo base_url(); ?&gt;";';
Then load your view and pass $data to it.

Now as long as your other js scripts get loaded AFTER where you echo the $js_vars in the &lt;head&gt;, you can use base_url within them and it will be dynamic. If you change $config['base_url'] in CI, the js will change automatically.

If I had to remember to change all of that hardcoded crap when I put it on a different server I think I'd shoot myself. This is much simpler and you only have 1 place you need to update base_url, which is how code should be.
#6

[eluser]boltsabre[/eluser]
Quote:$data['js_vars'] = 'var base_url = "&lt;?php echo base_url(); ?&gt;";';

Shouldn't that be without the "var" to make it global in JS? You won't be able to access it in functions unless you pass it manually otherwise???
#7

[eluser]CroNiX[/eluser]
No, if you define it outside a function/class, it's global as with most languages. Does it not work for you?
#8

[eluser]boltsabre[/eluser]
Well there you go, it works... obviously JS is not my strong suit and I should test stuff before posting comments ;-)
I was getting confused about globals inside and outside of functions!
#9

[eluser]GrahamDj28[/eluser]
Why don't you use navite JS to get the base url of your site?

window_DOT_location_DOT_host will give you the base url of your site

Code:
var base_url = 'http://'+window_DOT_location_DOT_host

replace _DOT_ with a .
#10

[eluser]jairoh_[/eluser]
@CroNiX solution made it. i helped. Smile




Theme © iAndrew 2016 - Forum software by © MyBB