Some methods for JS operation of IFRAME introduction _javascript skills

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.

(JavaScript)

Copy Code code as follows:

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.

(JavaScript)

Copy Code code as follows:

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.

(JavaScript)

Copy Code code as follows:

var IFR = document.createelement (' iframe ');
IFR.SRC = ' http://www.jb51.net/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:

Copy Code code as follows:

<iframe onload= "alert (' Loaded ');" Src= "http://www.jb51.net/index.php" ></iframe>

Method Two:
Only IE supports passing such parameters for createelement
Copy Code code as follows:

var IFR = document.createelement (' <iframe onload= "alert (' Loaded ');" src= "http://www.jb51.net/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.

Copy Code code as follows:

var IFR = document.createelement (' iframe ');
IFR.SRC = ' http://b.jb51.net/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.

Copy Code code as follows:

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.