How to use base_url() in 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: How to use base_url() in js file (/showthread.php?tid=26954) Pages:
1
2
|
How to use base_url() in js file - El Forum - 01-27-2010 [eluser]keld[/eluser] Hi, I'm trying to use base_url() into a javascript file that I've renamed .js.php because I'm making an ajax request like this: Code: ... How do I make the base_url accessible from here? I can add this at the top of my myscript.js.php file Code: $this->load->helper('url'); The problem is that my js folder is outside the application folder. Thanks a lot! How to use base_url() in js file - El Forum - 01-27-2010 [eluser]Devon Lambert[/eluser] I typically print out the Base_URL to my pages via the following: Code: // Place the following above all other Javascript files/calls in either your Head section OR Then the base_url JS variable is available across all of your JS files. Hope this helps? How to use base_url() in js file - El Forum - 01-27-2010 [eluser]keld[/eluser] I placed your code in the head section of my template page, before my call to the js file. In my js file i modify my code to: Code: $.post(<?php echo base_url; ?>.'/admin/dashboard/getStuff', {var: qt, ajax:'1'},... How to use base_url() in js file - El Forum - 01-27-2010 [eluser]davidbehler[/eluser] You can't use php code inside js files. What dnyce meant is this: Place this code in the header of your page Code: <scr1pt type="text/javascr1pt"> Code: $.post(base_url + '/admin/dashboard/getStuff', {var: qt, ajax:'1'},... How to use base_url() in js file - El Forum - 01-27-2010 [eluser]Devon Lambert[/eluser] Haha waldmeister: You beat me to it. That is precisely how I've done it in the past. How to use base_url() in js file - El Forum - 01-27-2010 [eluser]keld[/eluser] Ok thanks it's working except i had to do this in my header instead: Code: <scr1pt type="text/javascr1pt"> How to use base_url() in js file - El Forum - 01-27-2010 [eluser]cahva[/eluser] I may be wrong but it could be just the missing quotes in the previous examples? Code: var base_url='<?php echo base_url() ?>'; ..because normally that CDATA is not needed. How to use base_url() in js file - El Forum - 01-27-2010 [eluser]keld[/eluser] [quote author="cahva" date="1264655514"]I may be wrong but it could be just the missing quotes in the previous examples? Code: var base_url='<?php echo base_url() ?>'; ..because normally that CDATA is not needed.[/quote] Yeah I don't think it's needed either I just remembered this example on my CI book and it worked so that's cool. How to use base_url() in js file - El Forum - 01-30-2010 [eluser]Đaяк Đaηтє[/eluser] Is no possible to use base_url() directly in js file... maybe this can be helpful: Code: //myjsfile.js.php But i´m not completly sure.... How to use base_url() in js file - El Forum - 02-02-2012 [eluser]obay[/eluser] [quote author="Đaяк Đaηтє" date="1264843123"]Is no possible to use base_url() directly in js file... maybe this can be helpful: Code: //myjsfile.js.php But i´m not completly sure....[/quote] You can't do that because the .js.php is saved outside the application/ folder wihle the base_url() is inside the application/ folder hence it can't find the base_url() helper. I tried moving the .js.php file inside the application/views/ folder, but it still doesn't work. Trying to access the .js.php in there gives "Access forbidden! You don't have permission to access the requested object. It is either read-protected or not readable by the server." Any ideas, anyone? |