Welcome Guest, Not a member yet? Register   Sign In
base_url() and javascript
#1

[eluser]0plus1[/eluser]
I have the need to mantain a project in two direction, a sandbox copy and a live version. The live version has a different directory structure so in javascript I need to change all the ajax calls:

Quote:/sandbox/controller/method

to

Quote:/controller/method

all this useless work could be simplified by using:

Quote:<?=base_url();?>controller/method

Problem is that the js is in a javascript file.

Is there a reccomended way to use base_url with javascript? I understand I could use: javascript.php problem is that with codeigniter I have to create a controller then a view.

Are there workaround / elegant way to achieve this?

Thank you
#2

[eluser]rogierb[/eluser]
I have a main template file and in the header I declare a global js var called $base_url. In that var I load the base_url() value.

In js I can now use
Code:
my_url = $base_url + 'anything/I/want/to/address'

Antoher way would be to use
Code:
<BASE HREF="URI">
#3

[eluser]khagendra[/eluser]
Suppose there is a .js file in a .php file then define a javascript variable that can be used as base_url in.js file. Here is an example


var baseURL = '<?=base_url()?>';

Now, baseURL can be used as CI base_url in file.js

Enjoy! :-)
#4

[eluser]0plus1[/eluser]
[quote author="khagendra" date="1256746096"]Suppose there is a .js file in a .php file then define a javascript variable that can be used as base_url in.js file. Here is an example


var baseURL = '<?=base_url()?>';

Now, baseURL can be used as CI base_url in file.js

Enjoy! :-)[/quote]

You're a genious.. I never tought of this! THANK YOU
#5

[eluser]CroNiX[/eluser]
You can also do this with external javascript files if you name them with a php extension instead of js. Then the first line of the js file should output a correct text/javascript header. You can do the same with css and have dynamic css properties. Much easier to define a color once in php and reuse it all over in css so you only have to change 1 value/color.




Theme © iAndrew 2016 - Forum software by © MyBB