参考自 jQuery的插件jquery.include.pack jqAjax = { //加载完成后的回调函数 callback: function () { }, //Main getScript: function (url) { if (batm.UtilAjax) { var head = document.getElementsByTagName("head")[0] || document.documentElement; if (this.getType(url).tag == 'script') { this.readyScript(head, url); } if (this.getType(url).tag == 'link') { this.readyCSS(head, url); } return true } }, //加载css readyCSS: function (head, url) { var css = this.createElement(url, "link"); if (css) { head.appendChild(css); this.Myload(head, css); return css; } else { return false; } }, //加载js readyScript: function (head, url) { var js = this.createElement(url, "script"); if (js) { head.appendChild(js); this.Myload(head, js); return js; } else { return false; } }, //加载成功后释放内存及执行方法 Myload: function (head, obj) { this.done = false; obj.onload = obj.onreadystatechange = function () { if (!this.done && (!this.readyState || this.readyState === "loaded" || this.readyState === "complete")) { this.done = true; if (typeof this.callback == 'function') { this.callback(); } //IE内存泄露 obj.onload = obj.onreadystatechange = null; if (head && obj.parentNode) { head.removeChild(obj); } } }; }, //创建元素 createElement: function (filename, filetype) { switch (filetype) { case 'script': { var scriptTag = document.createElement(filetype); scriptTag.setAttribute('language', 'javascript'); scriptTag.setAttribute('type', 'text/javascript'); scriptTag.setAttribute('src', filename); return scriptTag } break; case 'link': { var styleTag = document.createElement(filetype); styleTag.setAttribute('type', 'text/css'); styleTag.setAttribute('rel', 'stylesheet'); styleTag.setAttribute('href', filename); return styleTag } break; default: return false; break } }, //获取加载文件的类型 getType: function (data) { data = data.replace(/^/s|/s$/g, ""); var m; if (//./w+$/.test(data)) { m = data.match(/([^]+)/.(/w+)$/); if (m) { if (m[2] == 'js') { return { filename: m[1], ext: m[2], tag: 'script' } } else if (m[2] == 'css') { return { filename: m[1], ext: m[2], tag: 'link' } } else { return { filename: m[1], ext: m[2], tag: null } } } else { return { filename: null, ext: null } } } else { m = data.match(/([^]+)$/); if (m) { return { filename: m[1], ext: null, tag: null } } else { return { filename: null, ext: null, tag: null } } } } }; 调用方法 jqAjax.getScript("jQuery.js");