Contentwindow,contentdocument, Docuemnt, Window,self,top,parent,opener's relationship

Source: Internet
Author: User

Objective

Recently began to develop the company's back-office management system, because the entire page of the schema is through the IFRAME to partition the region, so often need to call the child window method through the parent window or child window to get the parent window variables, such as window, so tidy up the relevant usage, avoid forgetting!

I. Differences and relationships between Top,parent,self,window,opener self: a reference to the current window

is equivalent to winow,window.self.

Top: Upper-level window pair image

equivalent to window.top; if the window itself is at the top level, the top is equivalent to the Window,window.top object.

Parent: Parents window of the current window

equivalent to window.parent; if the window itself is at the top level, the top is equivalent to the Parent,window.parent object.

Opener: property is a readable and writable property that returns a reference to the Window object that created the current windows;

Syntax: Window.opener

Description

    • Window.opener refers to the window of a new window opened by the Window.Open method, for example: A window through the Wndow.open method (not by other means), open b window, then Windowb.opener = = WindowA;
    • Window.opener is not directly related to Window.parent, Window.opener is equivalent to window.parent only if the iframe window is the parent window that is opened by window.open (Url,framename)!

Give me a chestnut: (I've been puzzled for a long time, embarrassed)

A.html

<!DOCTYPE HTML><HTMLLang= "en">    <Head>        <MetaCharSet= "UTF-8">        <title>Document</title>        <Scripttype= "Text/javascript"SCR=".. /jquery1.10.2.min.js "></Script>    </Head>    <Body>        <iframeID= "Contframe"name= "Contframe"></iframe>    </Body>    <Scripttype= "Text/javascript">window.open ("B. html","Contframe")    </Script></HTML>

B.html

<!DOCTYPE HTML><HTMLLang= "en">    <Head>        <MetaCharSet= "UTF-8">        <title>Document</title>    </Head>    <Body>        <P>I was the page that was opened</P>    </Body>    <Scripttype= "Text/javascript">        varequalable=Window.opener==window.parent; Console.log (equalable);//true    </Script></HTML>

In this case, Window.opener = = window.parent;

Two. How to get contentwindow,contentdocument and IFRAME

Defined:

Contentwindow: The Window object of the Subwindow, all major browsers support the Contentwindow attribute, but he is not part of the standard;

contentdocument : The Document object for the child window , returns the document contained by the frame as an HTML object. all the standard DOM methods can be used to process the returned object, IE needs ie8+ to support,

Get IFRAME:

There are 2 ways to get an IFRAME element:

1.document.getelementbyid ("Iframeid")

2.window.frames["Iframename"]

The IFrame object obtained in the above 2 ways to get the window and document objects representing the current IFrame windows also corresponds to 2 ways.

Get Window,document object:

If the IFRAME is obtained directly from the DOM, the way to get docuemen and window is:

doc= document.getElementById (' Iframename ' ). contentdocument;//Document objectwind = document.getElementById (' Iframename '). Contentwindow;  Window object

If the IFRAME is acquired by window.iframes["name", the way to get docuemen and window is:

doc = document.frames[' Iframeid '= window.frames["Iframeid"].window;

difference: When combing the knowledge of the IFRAME, I think contentwindow and window may be different, after combing to realize that the doubt itself is problematic, Contentwindow is the window object of the Subwindow, in the child window , whose window object is indicated by the keyword window, pointing to its windowed reference, in the parent window, you can get the window object of the child window through the Contentwindow property. It's just a different way of saying it! It seems that combing the knowledge is very useful!

Reference Documentation:

Http://www.runoob.com/jsref/prop-frame-contentwindow.html

Http://www.jb51.net/article/39489.htm

Contentwindow properties are supported for all major browsers

Contentwindow,contentdocument, Docuemnt, Window,self,top,parent,opener's relationship

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.