Home > Web Front-end > JS Tutorial > body text

The perfect way to determine whether an iframe is loaded_javascript skills

WBOY
Release: 2016-05-16 18:37:02
Original
1071 people have browsed it
Copy code The code is as follows:

var iframe = document.createElement("iframe");
iframe.src = "http://www.jb51.net";

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 comment on Nicholas C. Zakas article "Iframes, onload, and document.domain" A new judgment method (perfect):

Copy code The code is as follows:

var iframe = document.createElement("iframe");
iframe.src = "http://www.jb51.net";

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);


Click for additional explanation:

IE supports iframe's onload event , but it is invisible and needs to be registered through attachEvent.
The second method is more perfect than the first method because the readystatechange event has some potential problems compared to the load event.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!