loadscript function page

//Steve Souders - http://www.stevesouders.com/blog/
//Dustin Diaz - http://www.dustindiaz.com/scriptjs/
window.loadscript = function() {
    if (!arguments.length) return;

    var head = document.getElementsByTagName('head')[0];
    var callback = null;
    var scripts = 0;
    var count = 0;

    //Loop through arguments
    for (var i = 0, len = arguments.length; i < len; i++) {
        var arg = arguments[i];
        
        //Load args up to the callback, rest ignored
        if (typeof(arg) === 'function') {
            callback = arg;
            scripts = i;
            break;
        }

        var el = document.createElement('script');
        el.type = 'text/javascript';

        el.onload = el.onreadystatechange = function () {
            if (el.readyState && el.readyState !== 'complete' 
&& el.readyState !== 'loaded') return false;
            
            el.onload = el.onreadystatechange = null;
            count++;
            if (callback != null && count === scripts) callback();
        };

        el.async = true;
        el.src = arg;
        head.appendChild(el);
    }
};

One thought on “loadscript function page

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s