Communication between subwindows and parent windows

Source: Internet
Author: User

Parent:

Function showchild ()
{
VaR url = "test. aspx ";
VaR width = 600;
VaR Height = 500;
VaR form = Document. All. form1;
VaR retvalue = Window. showmodaldialog (URL, "", "dialogwidth:" + width + "PX; dialogheight:" + height + "PX; Status: Yes; directories: Yes; scrollbars: yes; resizable = yes ;");
If (retvalue! = NULL)
{
Document. getelementbyid ("txt_var1"). value = retvalue [0];
Document. getelementbyid ("txt_var2"). value = retvalue [1];
// Form. var2.value = retvalue [2];
}
}

Function returnvalue ()
{
VaR txtvalue = Document. getelementbyid ("text1 ");
Window. returnvalue = retarr;
Self. Close ();
}

(1) Use window. open () to create a window and communicate with the parent window
You can obtain the parent window object through window. opener on the subwindow page. After obtaining the object, the subwindow can refresh the parent window and pass the value.
For example:
Window. opener. Location. Reload (); // refresh the parent window in the subwindow.
Window. opener. Location. href // obtain the href of the parent window.
Window. opener. locaiton. pathname // obtain the parent window path

// Refresh the parent page
Window. Location. href = Window. Location. href; // locate the parent page
Window. Location. Reload;

(2) Communication Between the modal window and the parent window
When you want to communicate with a parent window created using showmodeldialog () and showmodelessdialog (), you cannot use window. Opener

To obtain the parent window object. To implement communication, you must input a parent window object to the Child Window when creating a modal subwindow.

Implementation Method:

In the parent window:

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:

:

VaR parent = widnow. dialogarguments;
The parent variable is the parent window object.

For example:

// 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:

// 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:

// Obtain the value from the subwindow
VaR newwin = Window. showmodeldialog (URL, window ,'');
If (newwin! = NULL)
Document. getelementbyid ("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:

VaR parent = Window. dialogarguments;
VaR xforwardedparent.doc ument. getelementbyid ("Age"). value;
X = x + 1;
// Set the age attribute value in the parent window
Parent.doc ument. getelementbyid ("Age"). value = X;

 

Other articles

Implementation Scheme of closing and refreshing the parent window after submitting the child page in Javascript

Http://blog.sina.com.cn/s/blog_4aa30443010008gy.html

Subwindow creation and communication between parent window and Child Window

Http://hi.baidu.com/lin206/blog/item/8d126d54944f7950564e0016.html

Showmodaldialog/showmodelessdialog instance. The parent window transmits a value to the Child window. The Child Window sets the value of the parent window. When the child window is closed, the return value is returned to the parent window. Close and refresh the parent window.
Http://www.cnblogs.com/adandelion/archive/2005/10/26/262666.html

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.