Welcome Guest, Not a member yet? Register   Sign In
How to use base_url() in js file
#1

[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:
...
var qt=$(this).find('select[name=fieldname]').val();
$.post(<?php echo base_url(); ?>.'/admin/dashboard/getStuff', {var: qt, ajax:'1'},...
...

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!
#2

[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
// Just before your closing </body> tag. Remember to wrap it in proper script tags
<?php echo "base_url = ". base_url(); ?>

Then the base_url JS variable is available across all of your JS files.

Hope this helps?
#3

[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'},...
And it still goes to the wrong url
#4

[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">
&lt;?php echo "var base_url = ". base_url(); ?&gt;
</scr1pt>
and then use it like this
Code:
$.post(base_url + '/admin/dashboard/getStuff', {var: qt, ajax:'1'},...
#5

[eluser]Devon Lambert[/eluser]
Haha

waldmeister: You beat me to it.

That is precisely how I've done it in the past.
#6

[eluser]keld[/eluser]
Ok thanks it's working except i had to do this in my header instead:
Code:
<scr1pt type="text/javascr1pt">
    // < ![CDATA[
           base_url = '&lt;?= base_url();?&gt;';
    //]] >
</scr1pt>
or it wouldn't work
#7

[eluser]cahva[/eluser]
I may be wrong but it could be just the missing quotes in the previous examples?
Code:
var base_url='&lt;?php echo base_url() ?&gt;';

..because normally that CDATA is not needed.
#8

[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='&lt;?php echo base_url() ?&gt;';

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

[eluser]Đaяк Đaηтє[/eluser]
Is no possible to use base_url() directly in js file... maybe this can be helpful:

Code:
//myjsfile.js.php
header ("content-type: text/javascript; charset: UTF-8");
var base_url='&lt;?php echo base_url() ?&gt;';
....

But i´m not completly sure....
#10

[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
header ("content-type: text/javascript; charset: UTF-8");
var base_url='&lt;?php echo base_url() ?&gt;';
....

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?




Theme © iAndrew 2016 - Forum software by © MyBB