判断 iframe 是否加载完成的完美方法

很老的东西,记下来下,万一以后要用到

一般来说,我们判断 iframe 是否加载完成其实与 判断 JavaScript 文件是否加载完成 采用的方法很类似:

 1 var iframe = document.createElement("iframe"); 
2 iframe.src = "http://www.planabc.net";
3 if (!/*@cc_on!@*/0) { //if not IE
4 iframe.onload = function(){
5 alert("Local iframe is now loaded.");
6 };
7 }
8 else {
9 iframe.onreadystatechange = function(){
10 if (iframe.readyState == "complete"){
11 alert("Local iframe is now loaded.");
12 }
13 };
14 }
15 document.body.appendChild(iframe);

最近, Nicholas C. Zakas 文章《Iframes, onload, and document.domain》的评论中 Christopher 提供了一个新的判断方法(很完美):

 1 var iframe = document.createElement("iframe"); 
2 iframe.src = "http://www.planabc.net";
3 if (iframe.attachEvent){
4 iframe.attachEvent("onload", function(){
5 alert("Local iframe is now loaded.");
6 });
7 }
8 else {
9 iframe.onload = function(){
10 alert("Local iframe is now loaded.");
11 };
12 }
13 document.body.appendChild(iframe);
原文地址:https://www.cnblogs.com/DoNetCShap/p/2287637.html