In general, the method used to determine whether iframe is loaded is similar to that used to determine whether JavaScript files are loaded successfully:
Copy to ClipboardReference: [www.bkjia.com] var iframe = document. createElement ("iframe ");
Iframe. src = "http://www.bkjia.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 method for judging the comment in the document "Iframes, onload, and document. domain" in Nicholas C. Zakas:
Copy to ClipboardReference: [www.bkjia.com] var iframe = document. createElement ("iframe ");
Iframe. src = "http://www.bkjia.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.