首頁  >  文章  >  web前端  >  javascript loadScript非同步載入腳本範例講解_jquery

javascript loadScript非同步載入腳本範例講解_jquery

WBOY
WBOY原創
2016-05-16 17:15:131732瀏覽

一、語法:
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()}}}}}}
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn