Introduction to the OnLoad event usage in IE under IFRAME

Source: Internet
Author: User
Tags object model

IE support the onload incident of IFrame, but is invisible, need to register through attachevent.
The second method is more perfect than the first method (using ReadyStateChange judgment) because there are some potential problems with the ReadyStateChange event relative to the Load event.

The feeling here is not completely accurate, beginning to cause me a lot of trouble. See its code to understand, the real meaning of IE in the creation of a new IFRAME when the OnLoad method needs to use the attachevent to bind, and the original IFrame of the OnLoad method, you can directly bind.
Say a little messy, everyone look at the code, a look will know:

The code is as follows Copy Code
<iframe id= ' Google ' ></iframe>
<script type= ' Text/javascript ' >
document.getElementById (' Google '). src= ' http://www.111cn.net/';
document.getElementById (' Google '). onload = function () {
Alert (' I am Google frame, now loaded ');
}
</script>


Here, also referred to the original text of the "Judge whether the completion of the perfect method of iframe" excerpt from this

The code is as follows Copy Code

var iframe = document.createelement ("iframe");
IFRAME.SRC = "Http://www.111cn.net";

if (iframe.attachevent) {
Iframe.attachevent ("onload", function () {
Alert ("Local iframe are now loaded.");
});
} else {
Iframe.onload = function () {
Alert ("Local iframe are now loaded.");
};
}

Document.body.appendChild (IFRAME);


IFRAME ONLOAD Event
In Firefox/opera/safari, the OnLoad event that uses the FRAME element directly:

The code is as follows Copy Code
document.getElementById ("Iframe1"). Onload=function () {
Your codes here.
};

Unfortunately it is often invalid under IE, because under IE it can only be activated once, and no matter how many iframe you have, the activated can only be the last one.


Reason
These events are activated in the Document object model within the IFRAME, not the parent page. When the IFRAME is loaded, the event is activated and the readystate is already in the "done" state. So you can't go through this event to check if an IFRAME is loaded.
To get a better performance, we have a little bit of a problem: the IFRAME recursion.

3.IFRAME recursion
When dealing with an IFRAME, the browser should have a basic rule that prevents recursion, prevents the page from loading indefinitely, and crashes the client device. In fact, there are several browsers in the article to do this, but different browsers have different ways of handling.

Try the following code, respectively:

The code is as follows Copy Code
<iframe src= "" onload= "Finish ()" Name= "Iframe1" ></iframe>
<iframe src= "#hashonly" onload= "Finish ()" Name= "Iframe2" ></iframe>
<iframe src= "Search" onload= "Finish ()" Name= "Iframe3" ></iframe>
<iframe src= "http://anotherPage.com" onload= "Finish ()" Name= "Iframe4" ></iframe>

As a result, when the parent page loads, the IFRAME onload function above is activated in the Ie/opera/safari, and Firefox does not respond to the second. This is mainly because they differ in their handling of the prevention of recursion.
For URLs such as #hashonly and search, the browser is interpreted as the page itself. But the difference between hash and search is that changing search can form a new source, and changing the hash does not. Typically, the browser stops loading when it encounters the same source of IFrame inside the page, but safari loads more than once.
If the finish () function is written as follows:

The code is as follows Copy Code
var finsh=function () {alert ("onload from:" +THIS.SRC);}

The number of pop-up message pop-ups at run time is as follows:

ifm/brw:    ie    |    firefox    |    opera    |    Safari iframe1:    1     |        1       |      1       |      0 iframe2:    1     |        0       |      1       |      1 iframe3:    2      |       1       |       2      |      2 iframe4:    1      |       1       |       1      |      1 combined with the content presented by the page, you can see these browsers in dealing with recursion issues in some of the rules:

Firefox does not load anything in the IFRAME and activates the OnLoad event (possibly any event)
ie and opera will not load the page in the IFRAME, but will activate the OnLoad event. The
Safari (Windows version) loads the page in an IFRAME once and only once, and activates the Onlaod event and activates only the OnLoad event that is attached to the IFRAME on the parent page.
With regard to this section, it makes little sense to use an IFRAME only for page nesting, which is only shown if you are using to dynamically load/render an inner page, or if you are using form target form submission processing (not Ajax) for a good user experience, and require high browser compatibility. As a result of this section, in order to improve compatibility, it is best to point an iframe to an empty page--blank.html in advance because it behaves the same in 4 different browsers. If you do not want to load the page in advance, it will take a little more effort to determine the browser type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.