判斷 iframe 是否載入完畢
阿新 • • 發佈:2019-01-01
我能想到的有以下幾種方式:
方法一、jQuery load()
Js程式碼- var frm = document.getElementById('myiframe');
- $(frm).load(function(){ // 等iframe載入完畢
- dosomething();
- });
方法二、onreadystatechange
Js程式碼- var iframe = document.createElement("myiframe");
- iframe.src = "http://www.baidu.com"
- if (!/*@[email protected]*/0) { //如果不是IE,IE的條件註釋
- iframe.onload = function(){
- alert("Local iframe is now loaded.");
- };
- } else {
- iframe.onreadystatechange = function(){ // IE下的節點都有onreadystatechange這個事件
- if (iframe.readyState == "complete"){
- alert("Local iframe is now loaded."
- }
- };
- }
- document.body.appendChild(iframe);
方法三、attachEvent
Js程式碼- var iframe = document.createElement("iframe");
- iframe.src = "http://www.baidu.com";
- if (iframe.attachEvent){
- iframe.attachEvent("onload", function(){ // IE
- alert("Local iframe is now loaded.");
- });
- } else {
- iframe.onload = function(){ // 非IE
- alert("Local iframe is now loaded.");
- };
- }
- document.body.appendChild(iframe);