JavaScript determines whether an IFRAME is loaded successfully

Source: Internet
Author: User

I have already introduced the Window.event article about cross iframe, interested can look down,
Http://www.css-js.cn/javascript/ie-iframe-window-event.html

Each elem node of IE has a onreadystatechange event that is triggered each time a change in the Elem content is sent.
For example, content is loading loading will trigger, content loaded loaded will trigger, content load success complete will trigger,
This function also needs to work with readystate, which is an attribute owned by each elem on IE that is used to view the state at each trigger.

First add a onreadystatechange iframe.attachevent ("onreadystatechange") to the IFRAME, function () {//This event will also be triggered when the content is not loaded. So we have to judge the state//Sometimes it will be more strange readyState state will skip complete so we loaded state also to judge if (Iframe.readystate = = "complete" | | iframe.readyst     ate = = "loaded") {//code can be executed to show here that the success has been loaded///To clear out the event iframe.detachevent ("onReadyStateChange", Arguments.callee); Here is the callback function}});
First add a onreadystatechange iframe.attachevent ("onreadystatechange") to the IFRAME, function () {//This event will also be triggered when the content is not loaded. So we have to judge the state//Sometimes it will be more strange readyState state will skip complete so we loaded state also to judge if (Iframe.readystate = = "complete" | | iframe.readystate = = "Loaded") {//code can be executed to show here that the success has been loaded///To clear off the event iframe.detachevent ("onReadyStateChange", Arguments.callee);//Here is the callback function}} );
Edit code Reset Run code

In other non-IE browsers firefox,opera,chrome and other IFRAME will have a onload event, this event as long as the trigger said that the content has been loaded.

Iframe.addeventlistener ("Load", function () {//code can be executed here the description has been loaded successfully completed This.removeeventlistener ("Load", Argume          Nts.call, false); Here is the callback function}, FALSE);
Iframe.addeventlistener ("Load", function () {//code can be executed here the description has been loaded successfully completed This.removeeventlistener ("Load", Arguments.call, FALSE); Here is the callback function}, FALSE);
Edit code Reset Run code

Note: 1. If the IFRAME is dynamically created, this function must be placed in an IFRAME that is appendchild to the body, otherwise it will not be triggered
2. If it is static IFRAME general onload will not be triggered, causing the same reason is because the iframe loading too fast, so the above method is not suitable.

Related Article

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.