JS operation iframe Framework attributes and elements detailed

Source: Internet
Author: User


1. Get an IFRAME Window object

There are cross-domain access restrictions.

Chrome:iframeelement. Contentwindow
Firefox:iframeElement.contentWindow
Ie6:iframeElement.contentWindow
The article IFRAMEs, onload, and Document.domain said "he iframe element object has a property called Contentdocument that contains the" IFrame ' s Document object, so and can use the ParentWindow property to retrieve the Window object. This means that some browsers can get the window object of the IFRAME through IframeElement.contentDocument.parentWindow. But the tested Firefox, Chrome element.contentdocument objects have no parentwindow properties.

The code is as follows Copy Code
function Getiframewindow (Element) {
return Element.contentwindow;
return Element.contentwindow | | Element.contentDocument.parentWindow;
}

2. Obtain the Document object of the IFRAME
There are cross-domain access restrictions.
Chrome:iframeElement.contentDocument
Firefox:iframeElement.contentDocument
Ie:element.contentWindow.document
Note: IE has no iframeelement.contentdocument attribute.

The code is as follows Copy Code

var getiframedocument = function (Element) {
return Element.contentdocument | | Element.contentWindow.document;
};

3. The window object that gets the parent page in the IFRAME
There are cross-domain access restrictions.
Parent page: window.parent
Top Level page: Window.top
Applies to all browsers

4. Get the HTML tag of the IFRAME in the parent page
There are cross-domain access restrictions.
Window.frameelement (Type: htmlelement), suitable for all browsers

5. The onload event of the IFRAME
The OnLoad event is provided by non-IE browsers. For example, the following code in IE does not have a pop-up box.

The code is as follows Copy Code
var IFR = document.createelement (' iframe ');
IFR.SRC = ' http://www.b.com/index.php ';
Ifr.onload = function () {
Alert (' Loaded ');
};
Document.body.appendChild (IFR);

But IE seems to provide the OnLoad event, the following two ways will trigger the OnLoad
Method One:

The code is as follows Copy Code
<iframe onload= "alert (' Loaded ');" Src= "http://www.b.com/index.php" ></iframe>

Method Two:

The code is as follows Copy Code
Only IE supports passing such parameters for createelement
var IFR = document.createelement (' <iframe onload= "alert (' Loaded ');" src= "http://www.b.com/index.php" ></ Iframe> ');
Document.body.appendChild (IFR);

Because the IFRAME element is contained in the parent page, there are no cross-domain problems with the above methods.
In fact, IE provides the OnLoad event, but it must be bound using attachevent.

  code is as follows copy code
var IFR = Document.createelement (' iframe '); 
    ifr.src = ' http://b.a.com/b.php '; 
     if (ifr.attachevent) { 
        ifr.attachevent (' onload '),   function () {alert (' Loaded ');});  
   } else { 
        ifr.onload  = function () {alert (' Loaded ');};  
   } 
    document.body.appendChild (IFR);

6. Frames
Window.frames can be taken to frames in the page (iframe, frame, etc.), you need to note that the window object is taken, not the htmlelement.

The code is as follows Copy Code

var ifr1 = document.getElementById (' IFR1 ');
var ifr1win = window.frames[0];
Ifr1win.frameelement = = IFR1; True
Ifr1win = = IFR1; False

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.