一、語法:
loadScript(url[,callback])
或
loadScript(settings)
二、settings支援的參數:
url:腳本路徑
async:是否異步,預設false(HTML5)
charset:檔案編碼
cache:是否緩存,預設為true
success:載入成功後執行的函數,優先執行callback。
三、呼叫舉例:
程式碼如下:
程式碼如下:
程式碼如下:
程式碼🎜>//loadScript(url[,callback])
loadScript(“http://code.jquery.com/jquery.js”);
loadScript(“http://code.jquery.com/ jquery.js”,function(){
console.log(1)
});
//loadScript(settings)
loadScript({“url”:”http://code. jquery.com/jquery.js”,”async”:false,”charset”:”utf-8″,”cache”:false});loadScript({“url”:”http://code. jquery.com/jquery.js”,”async”:false,”charset”:”utf-8″,”success”:function(){
四、原始碼:
複製程式碼
程式碼如下:
function loadScript(url,call)
var head = document.head || document.getElementsByTagName(“head”)[0] || document.documentElement,
script,
options,
if (typeof url == (typeof url == (typeof url == (typeof url == = “object”) {
options = url;
url = undefined;
}
s = options || {};
url = url || s.url;
callback = callback || s.success;
script = document.createElement(“script”);
script.async = s.async || false;
script.type = “text/javascript”;
if (s.charset) {
script.charset = s.charset;
}
if(s.cache === false){
url = url ( /?/. test( url ) ? “&” : “?” ) “_=” (new Date()).getTime();
}script.src = url;head.insertBefore(script, head.firstChild);if(callback){document.addEventListener ? script.addEventListener(“load”, callback, false) : script.onreadystatechange = function() {if( /.test(script.readyState)) {script.onreadystatechange = nullcallback()}}}}}}