js下判斷 iframe 是否加載完成的完美方法
更新時間:2010年10月26日 13:43:44 作者:
一般來說,我們判斷 iframe 是否加載完成其實與 判斷JavaScript 文件是否加載完成。
采用的方法很類似:
var iframe = document.createElement("iframe");
iframe.src = "http://www.dhdzp.com";
if (!/*@cc_on!@*/0) { //if not IE
iframe.onload = function(){
alert("Local iframe is now loaded.");
};
} else {
iframe.onreadystatechange = function(){
if (iframe.readyState == "complete"){
alert("Local iframe is now loaded.");
}
};
}
document.body.appendChild(iframe);
最近, Nicholas C. Zakas 文章《Iframes, onload, and document.domain》的評論中 Christopher 提供了一個新的判斷方法(很完美):
var iframe = document.createElement("iframe");
iframe.src = "http://sc.jb51.net";
if (iframe.attachEvent){
iframe.attachEvent("onload", function(){
alert("Local iframe is now loaded.");
});
} else {
iframe.onload = function(){
alert("Local iframe is now loaded.");
};
}
document.body.appendChild(iframe);
幾點補充說明:
IE 支持 iframe 的 onload 事件,不過是隱形的,需要通過 attachEvent 來注冊。
第二種方法比第一種方法更完美,因為 readystatechange 事件相對于 load 事件有一些潛在的問題。
復(fù)制代碼 代碼如下:
var iframe = document.createElement("iframe");
iframe.src = "http://www.dhdzp.com";
if (!/*@cc_on!@*/0) { //if not IE
iframe.onload = function(){
alert("Local iframe is now loaded.");
};
} else {
iframe.onreadystatechange = function(){
if (iframe.readyState == "complete"){
alert("Local iframe is now loaded.");
}
};
}
document.body.appendChild(iframe);
最近, Nicholas C. Zakas 文章《Iframes, onload, and document.domain》的評論中 Christopher 提供了一個新的判斷方法(很完美):
復(fù)制代碼 代碼如下:
var iframe = document.createElement("iframe");
iframe.src = "http://sc.jb51.net";
if (iframe.attachEvent){
iframe.attachEvent("onload", function(){
alert("Local iframe is now loaded.");
});
} else {
iframe.onload = function(){
alert("Local iframe is now loaded.");
};
}
document.body.appendChild(iframe);
幾點補充說明:
IE 支持 iframe 的 onload 事件,不過是隱形的,需要通過 attachEvent 來注冊。
第二種方法比第一種方法更完美,因為 readystatechange 事件相對于 load 事件有一些潛在的問題。
您可能感興趣的文章:
- js動態(tài)創(chuàng)建上傳表單通過iframe模擬Ajax實現(xiàn)無刷新
- iframe子父頁面調(diào)用js函數(shù)示例
- js與jQuery 獲取父窗、子窗的iframe
- js iframe跨域訪問(同主域/非同主域)分別深入介紹
- js中iframe調(diào)用父頁面的方法
- js操作iframe的一些方法介紹
- js實現(xiàn)網(wǎng)頁防止被iframe框架嵌套及幾種location.href的區(qū)別
- JS中Iframe之間傳值及子頁面與父頁面應(yīng)用
- js實現(xiàn)iframe動態(tài)調(diào)整高度的代碼
- JavaScript實現(xiàn)同一頁面內(nèi)兩個表單互相傳值的方法
- jquery ajax提交表單從action傳值到j(luò)sp實現(xiàn)小結(jié)
- JS實現(xiàn)向iframe中表單傳值的方法
相關(guān)文章
JavaScript檢測字符串中是否含有html標簽實現(xiàn)方法
這篇文章主要介紹了JavaScript檢測字符串中是否含有html標簽實現(xiàn)方法,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-07-07
JavaScript forEach()遍歷函數(shù)使用及介紹
這篇文章主要介紹了JavaScript forEach()遍歷函數(shù)使用及介紹,本文講解了使用forEach遍歷數(shù)組的用法以及提前終止循環(huán)的一個方法技巧,需要的朋友可以參考下2015-07-07
基于javascript實現(xiàn)瀏覽器滾動條快到底部時自動加載數(shù)據(jù)
這篇文章主要介紹了基于javascript實現(xiàn)瀏覽器滾動條快到底部時自動加載數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2015-11-11
ES6標準 Arrow Function(箭頭函數(shù)=>)
ES6標準新增了一種新的函數(shù):Arrow Function(箭頭函數(shù)),為什么叫Arrow Function?因為它的定義用的就是一個箭頭2020-05-05

