欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js 判斷腳本加載完畢的代碼

 更新時(shí)間:2011年07月13日 16:37:46   作者:  
記錄一段代碼,用來判斷腳本是否加載完畢。
復(fù)制代碼 代碼如下:

if(this.isIE) {
js.onreadystatechange=function(){if(js.readyState=="loaded" || js.readyState=="complete") callback();}
}else{js.onload=function(){callback();}}
js.onerror=function(){alert('Not Found (404): '+src)}//chrome


JS判斷腳本是否加載完成

在“按需加載”的需求中,我們經(jīng)常會(huì)判斷當(dāng)腳本加載完成時(shí),返回一個(gè)回調(diào)函數(shù),那如何去判斷腳本的加載完成呢?
我們可以對加載的 JS 對象使用 onload 來判斷(js.onload),此方法 Firefox2、Firefox3、Safari3.1+、Opera9.6+ 瀏覽器都能很好的支持,但 IE6、IE7 卻不支持。曲線救國 —— IE6、IE7 我們可以使用 js.onreadystatechange 來跟蹤每個(gè)狀態(tài)變化的情況(一般為 loading 、loaded、interactive、complete),當(dāng)返回狀態(tài)為 loaded 或 complete 時(shí),則表示加載完成,返回回調(diào)函數(shù)。
對于 readyState 狀態(tài)需要一個(gè)補(bǔ)充說明:
在 interactive 狀態(tài)下,用戶可以參與互動(dòng)。
Opera 其實(shí)也支持 js.onreadystatechange,但他的狀態(tài)和 IE 的有很大差別。
具體實(shí)現(xiàn)代碼如下:
復(fù)制代碼 代碼如下:

function include_js(file) {
var _doc = document.getElementsByTagName('head')[0];
var js = document.createElement('script');
js.setAttribute('type', 'text/javascript');
js.setAttribute('src', file);
_doc.appendChild(js);
if (!/*@cc_on!@*/0) { //if not IE
//Firefox2、Firefox3、Safari3.1+、Opera9.6+ support js.onload
js.onload = function () {
alert('Firefox2、Firefox3、Safari3.1+、Opera9.6+ support js.onload');
}
} else {
//IE6、IE7 support js.onreadystatechange
js.onreadystatechange = function () {
if (js.readyState == 'loaded' || js.readyState == 'complete') {
alert('IE6、IE7 support js.onreadystatechange');
}
}
}
return false;
}
//execution function
include_js('http://www.dbjr.com.cn/jslib//jquery/jquery.js');

相關(guān)文章

最新評論