Differences between window. parent and window. openner

Source: Internet
Author: User

Today, we will summarize the differences and usage of several objects in javascript:
First, the usage of parent. window and top. window
"Window. location. href" and "location. href" indicate the current page Jump.
"Parent. location. href" is the previous page Jump
"Top. location. href" is the outermost page Jump
Example:
If A, B, C, and D are all jsp, D is the iframe of C, C is the iframe of B, and B is the iframe of A, if js in D is written like this
"Window. location. href", "location. href": D page Jump
"Parent. location. href": C page Jump
"Top. location. href": A page Jump
Now I understand the usage of target during connection:
_ Blank: open a new window
_ Parent: the parent window is redirected.
_ Self: Page redirection
_ Top: redirection of the first parent window
In conclusion, parent. window: parent window object top. window: first parent window object
Next, let's take a look at the differences between window. parent and window. openner.
Window. parent is the parent page object called by the iframe page. When we want to access the outer page from the embedded page of iframe, we can directly use window. parent to obtain it;
Example:
A.html
Copy codeThe Code is as follows:
<Html>
<Head>
<Title> parent page </title>
</Head>
<Body>
<Form id = "form1" action = "">
<Div>
Input value:
<Input type = "text" name = "username" id = "username"/> <br/>
<Iframe src = "B .html" width = "400px" height = "300px"> </iframe>
</Div>
</Form>
</Body>
</Html>

B .html
Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = "text/javascript">
Function getpValue ()
{
Document. getelementbyidx_x_x ("span1" 2.16.innertext?##parent.doc ument. getelementbyidx_x_x ("username"). value;
}
</Script>
</Head>
<Body>
<Span> text box value: </span> <span id = "span1"> </span> <br/>
<Input type = "button" value = "get the text box value in the parent window" onclick = "getpValue ();">
</Body>
</Html>

Window. opener is a child page opened by window. open or hyperlink <a>. It calls the parent page object.
Example:
A.html
Copy codeThe Code is as follows:
<Html>
<Head>
<Title> parent page </title>
<Script type = "text/javascript">
Function openB ()
{
Required parameter open(' B .html ',' B ', 'width = 400, height = 200, status = no, toolbar = no, menubar = no, location = no, resizable = yes, left = 200, top = 100 ');
}
</Script>
</Head>
<Body>
<Form id = "form1" action = "">
<Div>
Input value:
<Input type = "text" name = "username" id = "username"/> <br/>
<Input type = "button" value = "open window B" onclick = "openB ();"/> <br/>
<A href = "B .html" target = "_ blank"> the hyperlink opens page B. </a>
</Div>
</Form>
</Body>
</Html>

B .html
Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = "text/javascript">
Function getpValue ()
{
Document. getelementbyidx_x_x ("span1" 2.16.innertext?##opener.doc ument. getelementbyidx_x_x ("username"). value;
}
</Script>
</Head>
<Body>
<Span> text box value: </span> <span id = "span1"> </span> <br/>
<Input type = "button" value = "get the text box value in the parent window" onclick = "getpValue ();">
</Body>
</Html>

Below are some common examples:
Parent. window and top. window are usually used in the split page, namely, frameset or iframe.
Log out of the framework and return to login. aspx: parent. window. location = 'login. aspx 'or
Top. window. location = 'login. aspx'
Window. parent is often used in the framework,
Refresh: window. parent. location. reload (); or refresh a frame: window. parent. MainForm. location. reload ();
Obtain the element values of other frameworks: window. parent. MainForm. form1.text1. value;
Window. opener mainly obtains some pages that open their own pages through hyperlinks or window. open (), such as obtaining values and refreshing.
Refresh: window. opener. location. reload ();
Value: Required parameter opener.doc ument. getElement ("txtName"). value;
Back: top. playFrame. history. go (-1 );
Forward: top. playFrame. history. go (1 );
Refresh: top. playFrame. location. reload ();
To sum up, these objects are very practical.

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.