判斷 iframe 是否加載完畢


我能想到的有以下幾種方式:

 

方法一、jQuery load()

 

Js代碼   收藏代碼
  1. var frm = document.getElementById('myiframe');  
  2. $(frm).load(function(){                             //  等iframe加載完畢  
  3. dosomething();  
  4. });  

 

方法二、onreadystatechange

 

Js代碼   收藏代碼
  1. var iframe = document.createElement("myiframe");  
  2. iframe.src = "http://www.baidu.com";  
  3. if (!/*@cc_on!@*/0) { //如果不是IE,IE的條件注釋  
  4.     iframe.onload = function(){     
  5.         alert("Local iframe is now loaded.");  
  6.     };  
  7. else {  
  8.     iframe.onreadystatechange = function(){ // IE下的節點都有onreadystatechange這個事件  
  9.         if (iframe.readyState == "complete"){  
  10.             alert("Local iframe is now loaded.");  
  11.         }  
  12.     };  
  13. }  
  14. document.body.appendChild(iframe);  

 

方法三、attachEvent

Js代碼   收藏代碼
  1. var iframe = document.createElement("iframe");  
  2. iframe.src = "http://www.baidu.com";  
  3.   
  4. if (iframe.attachEvent){  
  5.     iframe.attachEvent("onload"function(){ // IE  
  6.         alert("Local iframe is now loaded.");  
  7.     });  
  8. else {  
  9.     iframe.onload = function(){ // 非IE  
  10.         alert("Local iframe is now loaded.");  
  11.     };  
  12. }  
  13.   
  14. document.body.appendChild(iframe);  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM