Home >Web Front-end >JS Tutorial >Analysis summary of onload method of IE iframe_javascript skills

Analysis summary of onload method of IE iframe_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:37:011259browse
The perfect way to determine whether the iframe is loaded
IE supports the onload event of iframe, but it is invisible and needs to be registered through attachEvent.
The second method is more perfect than the first method (using readystatechange to judge), because the readystatechange event has some potential problems compared to the load event.

I feel like what I said here is not completely accurate, and it started to cause me a lot of trouble. Only after looking at the code do we understand that in a true sense, IE's onload method when creating a new iframe needs to be bound using attachEvent, while the onload method of the existing iframe can be bound directly.

It’s a bit confusing. You can read the code and you will know at a glance:

Copy code Code As follows:




Here, we also excerpt the original article mentioned in the original article "The perfect way to determine whether the iframe is loaded."
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);
Statement:
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