CodeIgniter Forums
[SOLVED] Autoloading js scripts that use base_url() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: [SOLVED] Autoloading js scripts that use base_url() (/showthread.php?tid=25880)



[SOLVED] Autoloading js scripts that use base_url() - El Forum - 12-29-2009

[eluser]ilSignorCarlo[/eluser]
Hi,
I often find myself writing functions inside controllers to load some jquery code just in certain pages.

For example:

Code:
function _load_create_post_js() {
        return '
        [removed]
        $("#tags").autocomplete(\''.base_url().'admin/tag/search_autocomplete\', {
        multiple: true,
        autoFill: true,
        multipleSeparator: " "
        });
        [removed]
        ';
    }

I thought to put all the scripts in a certain directory and then load them in an array, but some of these scripts use base_url().

Is there a nice solution for this?


[SOLVED] Autoloading js scripts that use base_url() - El Forum - 12-29-2009

[eluser]Pascal Kriete[/eluser]
Create a global js variable for base_url() at the top of the page.

So in a header view you might have:
Code:
<script type="text/javascript">
BASE_URL = "<?=base_url()?>";
</script>

And then in all subsequent scripts on the page you can just use BASE_URL as your basepath:
Code:
$("#tags").autocomplete(BASE_URL+"admin/tag/search_autocomplete", {



[SOLVED] Autoloading js scripts that use base_url() - El Forum - 12-29-2009

[eluser]saidai jagan[/eluser]
Yeah good answer


[SOLVED] Autoloading js scripts that use base_url() - El Forum - 12-29-2009

[eluser]ilSignorCarlo[/eluser]
[quote author="Pascal Kriete" date="1262113817"]Create a global js variable for base_url() at the top of the page.

So in a header view you might have:
Code:
<script type="text/javascript">
BASE_URL = "<?=base_url()?>";
</script>
[/quote]

Thanks, this works great.


[SOLVED] Autoloading js scripts that use base_url() - El Forum - 12-29-2009

[eluser]Andrew Cairns[/eluser]
This is a good way to define custom vars for JS.
If only we could do the same for CSS Smile