topogigiovanni
12/26/2018 - 3:39 PM

async load js

carregamento de assets async - ivan

try{
	eval('window.Async = function(f){return async function(arguments){f(arguments)}}');
}catch(e){
	eval('window.Async = function(f){return function(arguments){f(arguments)}}');
}

/*
Async(function(a,b,c){
	sdsd
	
})(a,b,c)
*/

(function(w,d,c){
	"use strict";

	var cssPreload 		= d.querySelectorAll('link[type="text/css"][rel="preload"]'),
		cssLoadCount 	= cssPreload.length;
	w['cssLoaded'] 		= (cssLoadCount===0);
	w['pageReady'] 		= !1;

	[].forEach.call(cssPreload, function(l){
		l.onload = function(){cssLoad(this)};
	});

	var evCssLoad, evPageReady;
	if (d.createEvent) {
		evCssLoad 	= d.createEvent("HTMLEvents");
		evPageReady	= d.createEvent("HTMLEvents");
		evCssLoad	.initEvent("cssload", true, true);		
		evPageReady	.initEvent("pageready", true, true);
	} else {
		evCssLoad	 			= d.createEventObject();
		evPageReady 			= d.createEventObject();
		evCssLoad.eventType 	= "cssload";
		evPageReady.eventType 	= "pageready";
	}
	evCssLoad.eventName 	= "cssload";
	evPageReady.eventName 	= "pageready";

	function cssLoad(l){
		if(l.rel!='stylesheet'){
			l.rel='stylesheet';
			cssLoadCount--;
			//console.log(cssLoadCount, evCssLoad);
			if(cssLoadCount<=0) cssLoadedTrigger();
		}
	}

	function cssLoadedTrigger(){
		if(w['cssLoaded']) return;
		if (w.dispatchEvent) {
			w.dispatchEvent(evCssLoad);
		} else {
			w.fireEvent("on" + evCssLoad.eventType, evCssLoad);
		}
		w['cssLoaded'] = !0;
		pageReadyTrigger();
		//c.log(w.cssLoaded, w.pageReady, d.readyState, evCssLoad);
	}

	function pageReadyTrigger(){
		if(w['pageReady'] || !w['cssLoaded'] || d.readyState!='complete') return;
		if (w.dispatchEvent) {
			w.dispatchEvent(evPageReady);
		} else {
			w.fireEvent("on" + evPageReady.eventType, evPageReady);
		}
		w['pageReady'] = !0;
		//c.log(w.cssLoaded, w.pageReady, d.readyState, evPageReady);
	}

	if (w.addEventListener) {
		d.addEventListener('readystatechange', pageReadyTrigger, true);
	} else if (window.attachEvent) {
		d.attachEvent('readystatechange', pageReadyTrigger);
	}

})(window,document,console);