Most of the time, we need to determine whether the iframe is fully loaded. If the load is complete, execute a js operation. In fact, the onload method can be used to determine whether iframe is loaded and whether the page is loaded. This article describes how to determine whether iframe is fully loaded by js.
var iframe = document.createElement("iframe"); iframe.src = "http://www.manongjc.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);
Recently, Christopher provided a new judgment method (perfect) in the comment on the document "Iframes, onload, and document. domain" in the document "Nicolas C. Zakas" Iframes, onload ):
var iframe = document.createElement("iframe"); iframe.src = "http://manongjc.com"; 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);
Additional Notes:
- IE supports iframe onload events, but they are invisible and need to be registered through attachEvent.
- The second method is more perfect than the first method, because the readystatechange event has some potential problems compared with the load event.