加载js并判断是否加载成功

在网上收集到的一段代码,我增加了指定挂载点的参数

function ScriptLoader() {}
ScriptLoader.prototype = {
	timer: function(
		times, // number of times to try
		delay, // delay per try
		delayMore, // extra delay per try (additional to delay)
		test, // called each try, timer stops if this returns true
		failure, // called on failure
		result // used internally, shouldn't be passed
	) {
		var me = this;
		if(times == -1 || times > 0) {
			setTimeout(function() {
				result = (test()) ? 1 : 0;
				me.timer((result) ? 0 : (times > 0) ? --times : times, delay + ((delayMore) ? delayMore : 0), delayMore, test, failure, result);
			}, (result || delay < 0) ? 0.1 : delay);
		} else if(typeof failure == 'function') {
			setTimeout(failure, 1);
		}
	},
	addEvent: function(el, eventName, eventFunc) {
		if(typeof el != 'object') {
			return false;
		}
		if(el.addEventListener) {
			el.addEventListener(eventName, eventFunc, false);
			return true;
		}
		if(el.attachEvent) {
			el.attachEvent("on" + eventName, eventFunc);
			return true;
		}
		return false;
	},
	// add script to dom
	require: function(url, args) {
		var me = this;
		args = args || {};
		var mountNode = args.mountNode || document.head || document.body || document.firstElementChild;
		var scriptTag = document.createElement('script');
		setTimeout(function() {
			var f = (typeof args.success == 'function') ? args.success : function() {};
			args.failure = (typeof args.failure == 'function') ? args.failure : function() {};
			var fail = function() {
				if(!scriptTag.__es) {
					scriptTag.__es = true;
					scriptTag.id = 'failed';
					args.failure(scriptTag);
				}
			};
			scriptTag.onload = function() {
				scriptTag.id = 'loaded';
				f(scriptTag);
			};
			scriptTag.type = 'text/javascript';
			scriptTag.async = (typeof args.async == 'boolean') ? args.async : false;
			scriptTag.charset = 'utf-8';
			me.__es = false;
			me.addEvent(scriptTag, 'error', fail); // when supported
			// when error event is not supported fall back to timer
			me.timer(15, 1000, 0, function() {
				return(scriptTag.id == 'loaded');
			}, function() {
				if(scriptTag.id != 'loaded') {
					fail();
				}
			});
			scriptTag.src = url;
			setTimeout(function() {
				try {
					mountNode.appendChild(scriptTag);
				} catch(e) {
					fail();
				}
			}, 1);
		}, (typeof args.delay == 'number') ? args.delay : 1);
		return true;
	}
};

使用方式

new ScriptLoader().require('xxx/js/main.js', {
    async: true, 
    success: function(){/*这里可以再次验证一下main.js里面定义的全局变量是否存在,双重确认加载成功*/},
    failure: function () {}
});

版权声明:
作者:倾城
链接:https://www.techfm.club/p/1050.html
来源:TechFM
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>