calling base_url() or site_url() in the external js file - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: calling base_url() or site_url() in the external js file (/showthread.php?tid=58640) Pages:
1
2
|
calling base_url() or site_url() in the external js file - El Forum - 07-02-2013 [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 calling base_url() or site_url() in the external js file - El Forum - 07-02-2013 [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 calling base_url() or site_url() in the external js file - El Forum - 07-02-2013 [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 yeah. tsk. anyway thankyou sir. calling base_url() or site_url() in the external js file - El Forum - 07-02-2013 [eluser]Syllean[/eluser] Pass it to your js function as a parameter calling base_url() or site_url() in the external js file - El Forum - 07-02-2013 [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> In your controller (or better yet, MY_Controller): Code: $data['js_vars'] = 'var base_url = "<?php echo base_url(); ?>";'; Now as long as your other js scripts get loaded AFTER where you echo the $js_vars in the <head>, 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. calling base_url() or site_url() in the external js file - El Forum - 07-03-2013 [eluser]boltsabre[/eluser] Quote:$data['js_vars'] = 'var base_url = "<?php echo base_url(); ?>";'; 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??? calling base_url() or site_url() in the external js file - El Forum - 07-03-2013 [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? calling base_url() or site_url() in the external js file - El Forum - 07-04-2013 [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! calling base_url() or site_url() in the external js file - El Forum - 07-04-2013 [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 . calling base_url() or site_url() in the external js file - El Forum - 07-04-2013 [eluser]jairoh_[/eluser] @CroNiX solution made it. i helped. |