Welcome Guest, Not a member yet? Register   Sign In
Using javascript files
#1

[eluser]diego.greyrobot[/eluser]
I love the way codeigniter handles URLs so that they are portable when deploying an app to a production server, however, that functionality does not translate easily to javascript unless the js code is on a php page.

How can a base_url variable (or any other for that matter) be passed into an external javascript file? That way i can take all of my javascript out of the head tags of my views (which are there purposefully in order to echo base_url where needed in the js).

Any ideas?
#2

[eluser]Colin Williams[/eluser]
You need to store the base_url() result in a JavaScript object. So, you really only need one PHP-fied javascript script to do this.

Code:
// This is JavaScript code
var Settings = {
  baseUrl : <?php print base_url() ?>
};
// End JavaScript code

And then in your other JavaScript files, you just need to get the Settings.baseUrl property
#3

[eluser]diego.greyrobot[/eluser]
Works like a charm! Thank you.
This is a much cleaner way of using dynamic js files.




Theme © iAndrew 2016 - Forum software by © MyBB