views:

33

answers:

2

I'm looking for a way to conditionally load and keep the execution order of some javascript files (external and internal) without any library dependency. Basically, what I want to do is load them up only if the browser supports localStorage.

Here's basically my shell:

if (window.localStorage) {
//load up JS only if it's needed
    var body = document.getElementsByTagName('body')[0],
        js1 = document.createElement('script'),
        js2 = document.createElement('script'),
        js3 = document.createElement('script'),
        js4 = document.createElement('script'),
        js5 = document.createElement('script');

    js1.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js';
    js2.src = 'http://www.google.com/jsapi';
    js3.src = 'my_file1.js';
    js4.src = 'my_file2.js';
    js5.src = 'my_file3.js';

    body.appendChild(js1);
    body.appendChild(js2);
    body.appendChild(js3);
    body.appendChild(js4);
    body.appendChild(js5);
} else { 
    //no localStorage support, display messaging
}

I've tried dynamically adding script nodes via createElement/body.appendChild but those don't seem to work.

Is there an easy way to achieve this? Right now everything works, but IE6 and IE7 folks download script they aren't even executing, which is what I want to fix.

+1  A: 
T.J. Crowder
I don't understand the callback. How would you load the following 2 external files http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js and http://www.google.com/jsapi as well as an internal JS file, say my_file1.js?
magenta
if (window.localStorage) { var body = document.getElementsByTagName('body')[0]; js1 = document.createElement('script'), js2 = document.createElement('script'), js3 = document.createElement('script'); js1.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'; js2.src = 'http://www.google.com/jsapi'; js3.src = 'my_file1.js'; body.appendChild(js1); body.appendChild(js2); body.appendChild(js3);}This doesn't work for me.
magenta
@magenta: Sorry, I assumed you were in control of the other scripts and could put the `doTheNextThing();` call in them (now I understand the "external and internal" part of the title!). One sec, there's another way.
T.J. Crowder
I edited my original question to show my code better.
magenta
@magenta: Added an option that does polling. Have to disappear, good luck with things.
T.J. Crowder
It certainly looks interesting and I'll give it a shot, but it's probably more complex than just loading up all the scripts regardless. I'll definitely give it a go, though - thanks much!
magenta
A: 

I've come to use this code. Both main functions (addEvent and load_javascript) are found on the web.
I wasn't trying to reduce download size, though: this is the only way I could load resources. So, maybe the idea proposed by Šime Vidas makes sense for you.

addEvent = function(elm, evType, fn, useCapture) {
    //Credit: Function written by Scott Andrews
    //(slightly modified)
    var ret = 0;

    if (elm.addEventListener) {
        ret = elm.addEventListener(evType, fn, useCapture);
    } else if (elm.attachEvent) {
        ret = elm.attachEvent('on' + evType, fn);
    } else {
        elm['on' + evType] = fn;
    }

    return ret;
};


    var left_to_load = 0;
    function init() {
        --left_to_load;
        if (left_to_load > 0) {
            return;
        }

        // all scripts are loaded now
        // proceed with your logic
    }

    // load js file and call function when done
    function load_javascript(src, callback) {
        var a = document.createElement('script');
        a.type = 'text/javascript';
        a.src = src;
        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(a, s);

        ++left_to_load;
        addEvent(a, 'load', callback, false);
    }



load_javascript('url1', init);
load_javascript('url2', init);
...
Nikita Rybak