[JavaScript] JavaScript handling of IFRAME actions

Source: Internet
Author: User

With the commanded, a few years ago the use of very frequent Frameset + frame has completed its mission of glorious discharge. As an alternative to frameset (let's say so), the use of IFrame is also more. The more frameset scenario, the IFRAME is more flexible to use and the document structure is more friendly.

This article on the JS operation IFrame in different browsers (yes, is also browser compatible ...) ) of the differences to do some explanation, and strive to summarize a suitable for all major browser solutions, the author only tested IE 6/7/8 (hereinafter referred to as IE) and Firefox 5.0 (hereinafter referred to as FF).

Conventions and Definitions

IFrameElement: Refers to the DOM element representation of an IFRAME, that is, a DOM object obtained by means of Document.getelemenetbyid () Iframeid: The property ID of the IFRAME, such as <iframe id= " Someid "> Iframename: Refers to the IFrame's property name, such as <iframe name=" Somename "> Iframeindex: The iframe index numbered from 0, if there are n frames in the page, The value range is 0–n-1 Iframewindow: Refers to the Window object of the IFRAME standard browser: a generic term for a Web browser, such as Firefox

one, the window object that gets the iframe in the parent page

Once you have the Window object, you can call the methods defined in the IFRAME page, and so on.

IE: Can be through Iframeid, Window.iframeid, Window.iframename, Window.frames[iframeid], Window.frames[iframename], Window.frames[iframeindex] and Iframeelement.contentwindow 6 ways to get the window object for the IFRAME.

FF: The Window object can be obtained through the 3 methods of Window.iframename, Window.frames[iframename] and Iframeelement.contentwindow.

Summary: To be compatible with most browsers, you should use Iframeelement.contentwindow to obtain them. See the following code:

  1. <iframe id="iframe1" name= "iframe1" src="frame1.html"></iframe >
  2. <script type="Text/javascript">
  3. Gets the window object for the IFRAME
  4. var iframe = document.getElementById (' iframe1 '). Contentwindow;
  5. </Script>
Second, get the document object of the IFRAME in the parent page

The standard browser can refer to the Doument object of the IFRAME via iframeelement.contentdocument, but IE browser (again this ... Does not support, the exact said should be IE 6/7, the author found in IE8 can already use this method to obtain.

Of course, because document is a sub-object of window, you can also get the window object of the IFrame and then refer to it through window.document.

Summary: The following two methods should be used to obtain, see Code:

  1. <iframe id="iframe1" src="frame1.html"></iframe>
  2. <script type="Text/javascript">
  3. Gets the document object for the IFRAME
  4. Method 1
  5. var iframe = document.getElementById (' iframe1 '). contentwindow.document;
  6. Method 2
  7. function Getiframedom (Iframeid) {
  8. return document.getElementById (Iframeid). contentdocument | | Window.frames[iframeid].document;
  9. }
  10. </Script>
iii. iframe page Gets the window object of the parent page

Parent: Father Page Window object

Top: Topmost page Window object

Self: A Window object that always points to the current page (equivalent to window)

For all browsers, when you get the Window object of the parent page, you can access the global variables and functions defined by the parent page, which is often used in the iframe interaction.

[JavaScript] JavaScript handling of IFRAME actions

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.