• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
calling base_url() or site_url() in the external js file

#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.


Messages In This Thread
calling base_url() or site_url() in the external js file - by El Forum - 07-02-2013, 08:26 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.