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 |