Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Autoloading js scripts that use base_url()
#1

[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?
#2

[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", {
#3

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

[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.
#5

[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




Theme © iAndrew 2016 - Forum software by © MyBB