动态加载javascript增强版

我们经常使用动态加载Javascript,写个函数很容易现实,之前也写过一个函数,不过当加载多个JS时,只能根据浏览器返回的顺序来先后加载,这肯定不是我们想要的,现在使用了一下技巧,当加载多个JS时,能按照我们的顺序加载:

 

Js代码 复制代码 收藏代码
  1. /* 
  2.     Name:动态加载JS 
  3.     Author:kingwell Leng 
  4.     Date : 2013/4/21 
  5.     E-mail: jinhua.leng##gmail.com 
  6.  
  7. */  
  8. function getScript(obj, callback) {  
  9.     var arr = obj,  
  10.     timeout,  
  11.     str = typeof obj === 'string';  
  12.     function add() {  
  13.         var script = document.createElement("script");  
  14.             header = document.getElementsByTagName("head")[0];  
  15.         script.src = str ? obj : arr[0];  
  16.         script.type = "text/javascript";  
  17.         if (str) {  
  18.             if (script.readyState) {  
  19.                 script.onreadystatechange = function () {  
  20.                     if (script.readyState === 'loaded' || script.readyState === 'complete') {  
  21.                         script.onreadystatechange = null;  
  22.                         callback && callback();  
  23.                     }  
  24.                 };  
  25.             } else {  
  26.                 script.onload = function () {  
  27.                     callback && callback();  
  28.                 };  
  29.             }  
  30.         } else {  
  31.             if (arr.length >= 1) {  
  32.                 if (script.readyState) {  
  33.                     script.onreadystatechange = function () {  
  34.                         if (script.readyState === 'loaded' || script.readyState === 'complete') {  
  35.                             script.onreadystatechange = null;  
  36.                             arr.shift();  
  37.                             timeout = setTimeout(add, 1);  
  38.                         }  
  39.                     };  
  40.                 } else {  
  41.                     script.onload = function () {  
  42.                         arr.shift();  
  43.                         timeout = setTimeout(add, 1);  
  44.                     };  
  45.                 }  
  46.             } else {  
  47.                 clearTimeout(timeout);  
  48.                 callback && callback();  
  49.             }  
  50.         }  
  51.         header.appendChild(script);  
  52.     }  
  53.     add();  
  54. }  
/*
    Name:动态加载JS
    Author:kingwell Leng
    Date : 2013/4/21
    E-mail: jinhua.leng##gmail.com

*/
function getScript(obj, callback) {
	var arr = obj,
	timeout,
	str = typeof obj === 'string';
	function add() {
		var script = document.createElement("script");
			header = document.getElementsByTagName("head")[0];
		script.src = str ? obj : arr[0];
		script.type = "text/javascript";
		if (str) {
			if (script.readyState) {
				script.onreadystatechange = function () {
					if (script.readyState === 'loaded' || script.readyState === 'complete') {
						script.onreadystatechange = null;
						callback && callback();
					}
				};
			} else {
				script.onload = function () {
					callback && callback();
				};
			}
		} else {
			if (arr.length >= 1) {
				if (script.readyState) {
					script.onreadystatechange = function () {
						if (script.readyState === 'loaded' || script.readyState === 'complete') {
							script.onreadystatechange = null;
							arr.shift();
							timeout = setTimeout(add, 1);
						}
					};
				} else {
					script.onload = function () {
						arr.shift();
						timeout = setTimeout(add, 1);
					};
				}
			} else {
				clearTimeout(timeout);
				callback && callback();
			}
		}
		header.appendChild(script);
	}
	add();
}

 

 

调用:

 

Js代码 复制代码 收藏代码
  1. getScript(['http://code.jquery.com/jquery-1.9.1.min.js','js1.js', 'js2.js', 'js3.js', 'js4.js'], function () {  
  2.     alert('我是按顺序加载啦');  
  3. });  
getScript(['http://code.jquery.com/jquery-1.9.1.min.js','js1.js', 'js2.js', 'js3.js', 'js4.js'], function () {
	alert('我是按顺序加载啦');
});

 现在是按照我们写代码的顺序加载啦...

posted @ 2014-07-10 15:21  super1234  阅读(514)  评论(2编辑  收藏  举报