|
可以批量加载js文件列表, 支持回调。 演示:http://designcss.org/public/jfk/source/tests/jsdemos/loadjs.html <script type="text/javascript"> ;(function(window){ function in_array(arr, value){ var i = 0, l = arr.length; for(; i < l; i++){ var val = arr[i]; if(val === value){ return true; }; }; return false; }; var opt = Object.prototype.toString; var Lib = {}; Lib.in_array = in_array; Lib.LoadJs = (function(){ var __files = []; return function(file, loaded) { var type = opt.call(file); if (arguments.length < 1) { return; }; var files = type === "[object String]" ? [file] : (type === "[object Array]" ? file: null); loaded = loaded || function(){}; if (files === null) { return }; if (files.length === 0) { return }; var filelen = files.length, dlen = 0, auto = null; down(dlen); auto = setInterval(function() { if (dlen === filelen) { clearInterval(auto); auto = null; loaded() } }, 10); function down(index) { if (index >= filelen) { return }; var doc = document, _body = doc.getElementsByTagName("body"); if (_body.length === 0) { return }; if (Lib.in_array(__files, files[index])) { dlen += 1; down(dlen) } else { _body = _body[0]; var _cache = doc.createDocumentFragment(); var script = doc.createElement("script"); script.charset = "utf-8"; script.src = files[index]; _cache.appendChild(script); _body.appendChild(_cache); if (doc.attachEvent) { script.attachEvent("onreadystatechange", function() { if (/complete|loaded/.test(script.readyState) === true) { __files.push(files[index]); dlen += 1; down(dlen) } }) } else { script.onload = function() { __files.push(files[index]); dlen += 1; down(dlen) } } } } } })(); window["Lib"] = Lib; })(window); </script> |
文明社会,从理性发言开始。谢绝地域攻击。