JS checks whether IFRAME is successfully loaded

Source: Internet
Author: User

In a project, you often need to dynamically add IFRAME, and then perform related operations on the added IFRAME. In most cases, IFRAME has not been added yet.CodeThe execution has been completed, so some things you have written are not displayed at all. At this time, we need to consider whether we can wait until the IFRAME is loaded and then perform the following operations. Of course, various browsers have long been taken into consideration for us. See the following:

IE browser

Each ELEM node of IE has an onreadystatechange event, which is triggered every time the ELEM content sends a change,For example, if the content is loading, it will be triggered after the content is loaded, and if the content is loaded successfully, complete will be triggered,This function also needs to work with readystate, which is an attribute of each ELEM on IE and used to view the status of each trigger.

// Add an onreadystatechangeiframe for IFRAME first. attachevent ("onreadystatechange", function () {// This event is also triggered when the content is not loaded, so we need to determine the status // sometimes it will be a strange readystate status and will skip the complete, so we also need to determine the loaded status if (IFRAME. readystate = "complete" | IFRAME. readystate = "loaded") {// the code can be executed here, indicating that the loading has been completed successfully. // you need to clear the event IFRAME. detachevent ("onreadystatechange", arguments. callee); // here is the callback function }});

Other browsers (Firefox, opera, chrome, etc.)

In other non-ie browsers, IFRAME, such as Firefox, opera, and chrome, will have an onload event. If this event is triggered, the name content has been loaded.

IFRAME. addeventlistener ("LOAD", function () {// the code can be executed here, indicating that the load has been completed successfully. This. removeeventlistener ("LOAD", arguments. call, false); // The callback function}, false );

Overall

 
If (IFRAME. attachevent) {IFRAME. attachevent ("onreadystatechange", function () {// This event is also triggered when the content is not loaded, so we need to determine the status // sometimes it will be a strange readystate status and will skip the complete, so we also need to determine the loaded status if (IFRAME. readystate = "complete" | IFRAME. readystate = "loaded") {// the code can be executed here, indicating that the loading has been completed successfully. // you need to clear the event IFRAME. detachevent ("onreadystatechange", arguments. callee); // here is the callback function});} else {IFRAME. addeventlistener ("LOAD", function () {// the code can be executed here, indicating that the load has been completed successfully. This. removeeventlistener ("LOAD", arguments. call, false); // The callback function}, false );}

Note:The above function must be placed after IFRAME is appended to the body, otherwise it will not be triggered


Reprinted from http://blog.csdn.net/huang100qi/article/details/7852377#reply

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.