-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.min.js
6 lines (6 loc) · 3.65 KB
/
load.min.js
1
2
3
4
5
6
/*
Load.js - JavaScript js/css, jsonp/ajax, sync/async loader
Docs and source: https://github.com/articobandurini/load.js
Distributed under MIT license.
*/
(function(b){var a=b.load=function(d){if(typeof d!=="object"||d instanceof Array){var c=a.args(arguments);d={url:c.url,callback:c.callback}}if(d.url&&d.url.length){if(typeof d.async==="undefined"){d.async=true}if(!d.type){d.type="js"}if(!(d.url instanceof Array)){d.url=[d.url]}a.sequence(d)}return a};a.sequence=function(e){var d=e.url.length,c=function(h){if(!h){h=1}d=d-h;if(!d&&typeof e.callback==="function"){e.callback.call(a)}},g=function(h){return h.length?(function(){c(h.length);a.sequence({url:h,async:e.async,type:e.type,callback:c})}):c};for(var f=0;f<e.url.length;f++){if(e.url[f] instanceof Array){a.sequence({url:e.url[f],async:e.async,type:e.type,callback:g(e.url.slice(f+1))});break}else{a.one({url:e.url[f],async:e.async,type:e.type,callback:c})}}return a};a.one=function(d){var c,f=false,e=document.getElementsByTagName("head")[0]||document.body;if(d.url in a.map){d.url=a.map[d.url]}if(d.type==="css"||d.url.toLowerCase().match(/\.css$/)){f=true;c=document.createElement("link");c.rel="stylesheet";c.href=a.path(d.url)}else{c=document.createElement("script");c.async=d.async;c.src=a.path(d.url)}e.appendChild(c);var g=function(i){if(typeof a.ready==="function"){a.ready.call(a,d.url)}if(typeof d.callback==="function"){d.callback.call(a)}if(!f&&i&&i.parentNode){i.parentNode.removeChild(i)}};var h=navigator.userAgent.match(/MSIE\s(\d+)/);if(h&&h[1]<11){c.onreadystatechange=function(){if(this.readyState==="loaded"||this.readyState==="complete"){g(this)}}}else{c.onload=function(){g(this)}}if(typeof d.errorHandler==="function"){c.onerror=function(i){d.errorHandler.call(a,i)}}return a};a.js=a.async=function(){var c=a.args(arguments);return a({url:c.url,callback:c.callback})};a.css=function(){var c=a.args(arguments);return a({url:c.url,callback:c.callback,type:"css"})};a.sync=function(){var c=a.args(arguments);return a({url:c.url,callback:c.callback,async:false})};a.jsonp=function(d,f,e,c){if(typeof f==="function"){if(!a.jsonp.index){a.jsonp.index=1}else{a.jsonp.index++}window["loadCallback"+a.jsonp.index]=f;d=d.replace("=?","=loadCallback"+a.jsonp.index)}return a.one({url:d,async:e!==false,type:"jsonp",errorHandler:c})};a.ajax=function(f,l,h,c,d){var k;if(!d){d={}}if(window.XMLHttpRequest){k=new XMLHttpRequest()}else{if(window.ActiveXObject){try{k=new ActiveXObject("Msxml2.XMLHTTP")}catch(j){try{k=new ActiveXObject("Microsoft.XMLHTTP")}catch(j){}}}}if(!k){return null}k.onreadystatechange=function(){if(k.readyState===4&&typeof l==="function"){l.call(k,k.responseText)}};if(typeof c==="function"){k.onerror=function(i){c.call(a,i)}}k.open(d.method||"GET",a.path(f),h!==false);if(d.headers){for(var g in d.headers){k.setRequestHeader(g,d.headers[g])}}k.send(d.data);return a};a.min=function(c,f){if(c&&typeof c==="string"){c=[c]}if((c instanceof Array)&&(typeof f==="function"?f.call(a):true)){for(var e=0;e<c.length;e++){var d="",g=/\.(js|css)$/i;if(c[e].match(g)){d=c[e].replace(g,function(h,j,k,i){return".min."+j})}else{d=c[e]+".min"}a.map[c[e]]=d}}return a};a.args=function(c){var d=Array.prototype.slice.call(c);return{url:d,callback:(typeof d[d.length-1]==="function")?d.pop():undefined}};a.path=function(c){return c.match(/^(https?\:|file\:|\/)/i)?c:a.root+c};a.init=function(){a.root="";a.map={};var f=document.getElementsByTagName("script"),d,e;for(var c=0;c<f.length;c++){if(f[c].src.match(/(^|\/)load(\.min)?\.js$/)||f[c].id==="load.js"){d=f[c].getAttribute("data-load");if(d){e=d.lastIndexOf("/")+1;a.root=e?d.substring(0,e):"";a({url:d.substring(e),async:f[c].getAttribute("data-async")!=="false"})}break}}};a.init()})(window);