Detailed description of the Code for calling the parent window program using Javascript

Source: Internet
Author: User

This article summarizes some common Javascript code used to call the parent window program. If you need to know it, you can refer to it.

Parent Page code:

The Code is as follows: Copy code

<! Doctype html public "-// IETF // DTD LEVEL1 // EN">
<Html>
<Head>
<Title> html.html </title>

<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "this is my page">
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
<! -- <Link rel = "stylesheet" type = "text/css" href = "./styles.css"> -->
</Head>
<Body>

<Div>
<Button id = "btn"> open new page </button>
</Div>

<Script>

Window. onload = function (){

Var btn = document. getElementById ("btn ");

Btn. onclick = openPage;

Function openPage (){
Try {
Required parameter open('newpage.html ');
} Catch (e ){
Alert (e );
}
// Alert ("OK ");
}
}
Function show (){
Document. title = new Date ();
}
</Script>
</Body>
</Html>

Page to be opened by newpage.html code

The Code is as follows: Copy code

<! Doctype html public "-// IETF // DTD LEVEL1 // EN">
<Html>
<Head>
<Title> newpage.html </title>

<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "this is my page">
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
<! -- <Link rel = "stylesheet" type = "text/css" href = "./styles.css"> -->
</Head>
<Body>
<Button id = "btn" onclick = "fun ()"> click </button>
<Script>

Function fun (){
Opener. show ();
}

</Script>
</Body>
</Html>

Close child window when closing parent window
Write in the parent window:

The Code is as follows: Copy code

 

Var sonid = window. open ('"
+ Url2 + "', 'newwindow', 'height = 108, width = 200, top ="
+ Top + ", left =" + left
+ ", Toolbar = no, menubar = no, scrollbars = no, resizable = no, location = no ,"
+ "Status = no, titlebar = no, z-look = 1, depended = yes, alwaysRaised = yes ')

Window. attachEvent ("onunload", closeSonwin );
Function closeSonwin ()
{
Sonid. close ();
}


Values assigned for mutual operation in the Child parent window


In the parent window:

The Code is as follows: Copy code

Var newWin = window. showModelDialog (url, window ,'');
NewWin. open ();

The window parameter is the parent window object.

In the subwindow:

You must first obtain the parent window object before using the parent window object. Because the parent window object is created in
The child window is passed in by passing in parameters. Therefore, in the Child window, you can only get the parent window object by getting window parameters. The method is as follows:

The Code is as follows: Copy code
Var parent = widnow. dialogArguments;

The parent variable is the parent window object.

For example:

The Code is as follows: Copy code

// Submit the form form1 in the parent window through the subwindow. After the subwindow is submitted, perform the query operation.
Var parent = window. dialogArguments;
Parent.doc ument. form1.action = "QueryInfor. jsp ";
Parent. submit ();

// Refresh the parent page
Var parent = window. dialogArguments;
Parent. location. reload ();

// Pass the value from the Child Window to the parent window
To realize the transfer of values in the modal subwindow to the parent window, use window. returnValue.

The implementation method is as follows:

In the subwindow:

The Code is as follows: Copy code

// Obtain the value of a field in the parent window. If you add one to the value, return to the parent window.
Var parent = window. dialogArguments;
Var xforwardedparent.doc uement. getElementById ("age"). value;
X = x + 1;

// Returns the x value.
Window. returnValue = x;

In the parent window:

The Code is as follows: Copy code

// Obtain the value from the subwindow
Var newWin = window. showModelDialog (url, window ,'');
If (newWin! = Null)
Document. getElementByIdx_x ("age"). value = newWin;

// Set the value of the parent window in the Child Window
The input value to the parent window in the Child Window does not seem to have been explicitly set in the parent window. Setting the element values in the parent window is more flexible, but the specific method used depends on the actual situation and the existing implementation method, if you use an unrealistic method, it not only reduces the development efficiency, but also reduces the execution efficiency. It will also lead to an inelegant implementation method and code style.

The sub-window uses the following methods to set the value of the parent window:

In the subwindow:

The Code is as follows: Copy code

Var parent = window. dialogArguments;
Var xforwardedparent.doc ument. getElementByIdx_x ("age"). value;
X = x + 1;
// Set the age attribute value in the parent window
Parent.doc ument. getElementByIdx_x ("age"). value = x;

Related Article

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.