Value Transfer between parent and child windows

Source: Internet
Author: User

Parent window: </P> <p> url = URL + '? Winflg = aaa & winpig = BBB '; </P> <p> window. open (URL, '',''); </P> <p> subwindow: </P> <p> var urlparams = new object (); <br/> var aparams = document. location. search. substr (1 ). split ('&'); <br/> for (VAR I = 0; I <aparams. length; I ++) {<br/> var aparam = aparams [I]. split ('='); <br/> urlparams [aparam [0] = aparam [1]; <br/>}</P> <p> alert (urlparams ["winflg"]); </P> <p> alert (urlparams ["winpig"]); </P> <p >======================== ========================================================== ========================================</P> <p> open a new window, this sub-window calls the method or variable of the parent object, which has not been clarified. I found some information on the Internet and summarized it: </P> <p> There are several methods to open a new window, window. open (...), window. showmodaldialog (...), and nested pages in IFRAME are also studied here, and there are also windows. navigate (...), window. location. href = "... ", window. history. back (-1); is implemented to allow page content jump, which is not discussed here. </P> <p> 1. Open subwindow: Use window. opener indicates the window object of the parent window </P> <p> 2. Modal subwindow: indirectly transfers the window object to the subwindow, then the child window can obtain the window object of the parent window </P> <p> 3. IFRAME subpage: Use window. parent indicates the window object of the parent window </P> <p> parent page: 1.htm code: </P> <p> <HTML> <br/> <pead> <br/> <title> open the Parent-Child Window to study data transfer-parent window </title> </P> <p> <MCE: SCRIPT> <! -- </P> <p> var parvalue = "the variable value in the parent window is displayed"; </P> <p> function test () {<br/> alert ("normal execution of methods in the parent window is displayed"); <br/>}</P> <p> // --> </MCE: SCRIPT> <br/> </pead> <br/> <body> <br/> <input type = "button" id = "mybutton1" value = "open a new window "onclick =" too many open('2.htm '); "> <br/> <input type =" button "id =" mybutton2 "value =" Open the modal window "onclick =" too many showmodaldialog('3.htm', window ); "> <br/> <IFRAME id =" subiframe "name = "Subiframe" src = "4.htm" mce_src =" 4.htm" scrolling = "Auto" frameborder = "1"> </iframe> <br/> </body> <br/> </ HTML> </P> <p> 2.htm code: </P> <p> <HTML> <br/> <pead> <br/> <title> open the Parent-Child Window to study data transfer-open the subwindow </title> <br/> <MCE: script Type = "text/JavaScript"> <! -- </P> <p> var buttonvalue?##opener.doc ument. getelementbyid ("mybutton2 "). value // obtain the object in the parent window <br/> var parentvalue = Window. opener. parvalue; // get the variable in the parent window </P> <p> function dopartest () {<br/> window. opener. test (); // call the method in the parent window </P> <p >}< br/> // --> </MCE: SCRIPT> <br/> </pead> <br/> <body> </P> <p> <input type = "button" value = "Open subwindow button" onclick = "alert ('buttonvalue: '+ buttonvalue); alert ('parentvalue:' + parentvalue ); Dopartest () "> </P> <p> </body> <br/> </ptml> </P> <p> 3.htm code: </P> <p> <HTML> <br/> <pead> <br/> <title> open the Parent-Child Window to study data transfer-open the modal subwindow </title> <br/> <MCE: script Type = "text/JavaScript"> <! -- </P> <p> var parentwin = Window. dialogarguments; <br/> var buttonvalue?parentwin.doc ument. getelementbyid ("mybutton2 "). value // obtain the object in the parent window <br/> var parentvalue = parentwin. parvalue; // obtain the variable in the parent window <br/> function dopartest () {<br/> parentwin. test (); // call the method in the parent window <br/>}< br/> // --> </MCE: SCRIPT> <br/> </pead> </P> <p> <body bgcolor = "# ffffff" text = "#000000"> </P> <p> <input type = "button" value = "Modal subwindow button" onclick = "alert ('Buttonvalue: '+ buttonvalue); alert </P> <p> ('parentvalue:' + parentvalue); parentwin. test (); "> <br/> </body> <br/> </ptml> </P> <p> 4.htm code: </P> <p> <HTML> <br/> <pead> <br/> <title> Study on transferring values in the parent and child windows-IFRAME neutron window </title> <br /> <MCE: script Type = "text/JavaScript"> <! -- </P> <p> var buttonvalue?##parent.doc ument. getelementbyid ("mybutton2 "). value // obtain the object in the parent window <br/> var parentvalue = Window. parent. parvalue; // get the variable in the parent window </P> <p> function dopartest () {<br/> window. parent. test (); // call the method in the parent window <br/>}< br/> // --> </MCE: SCRIPT> <br/> </pead> <br/> <body> </P> <p> <input type = "button" value = "iframe subwindow button" onclick = "alert ('buttonvalue: '+ buttonvalue); alert ('parentvalue:' + parentvalue); dopartest () "> </P> <p> </body> <br/> </ptml> </P> <p>

Below is a test example written by myself:
Test1:

<Input type = "text" id = "TXT"> <br/> <Div style = "float: Left;" mce_style = "float: left; "id =" Div "> </div> <br/> <input type =" button "value =" open "onclick =" OpenWindow () "> </P> <p> <MCE: Script Type =" text/JavaScript "> <! -- </P> <p> function OpenWindow () {</P> <p> var result = Window. showmodaldialog ("test2.html", window); <br/> alert (result); <br/> var arr = result. split ('_'); <br/> var arrcodes = arr [0]. split (','); <br/> var arrnames = arr [1]. split (','); <br/> document. getelementbyid ('txt '). value = arr [1]; </P> <p> var innerh = ''; <br/> for (VAR I = 0; I <arrcodes. length-1; I ++) {<br/> innerh + = '<input type =/"text/" name =/"Old/" value =/"' + arrcodes [I] + '/"/> '<br/>}< br/> document. getelementbyid ('div '). innerhtml = innerh; </P> <p >}< br/> // --> </MCE: SCRIPT> <br/>

 

Test2:

<HTML> <br/> <pead> <br/> <MCE: Script Type = "text/JavaScript"> <! -- </P> <p> function onload () {</P> <p> var parentwin = Window. dialogarguments; <br/> var OBJ = parentwin.doc ument. getelementsbyname ('Old'); <br/> var oldvalues = new array (); <br/> for (VAR I = 0; I <obj. length; I ++) {<br/> oldvalues [I] = OBJ [I]. value; <br/>}</P> <p> var OBJ = document. getelementsbyname ('selectvalues'); <br/> for (VAR I = 0; I <obj. length; I ++) {<br/> for (VAR J = 0; j <oldvalues. length; j ++) {<br/> If (OBJ [I]. value = oldvalues [J]) <br/> {OBJ [I]. checked = 'checked' ;}< br/>}</P> <p >}< br/>}</P> <p> function OK () {</P> <p> var industrycodes = new array (); <br/> var industrynames = new array (); </P> <p> var tmpcodes = ''; <br/> var tmpnames =''; <br/> var Codes = document. getelementsbyname ("selectvalues"); <br/> var names = document. getelementsbyname ("selectnames"); </P> <p> for (VAR I = 0, j = 0; I <codes. length; I ++) {<br/> If (codes [I]. checked) {<br/> tmpcodes + = codes [I]. value + ','; <br/> tmpnames + = Names [I]. value + ','; <br/> J ++; <br/>}</P> <p> var tmpvalue = tmpcodes + '_' + tmpnames; </P> <p> window. returnvalue = tmpvalue; <br/> window. close (); <br/>}< br/> // --> </MCE: SCRIPT> <br/> </pead> </P> <p> <body onload = "onLoad () "> <br/> <input type =" text "id =" test1 "value =" "/> <br/> <input type =" checkbox "name =" selectvalues" value = "1"/> text 1 <input type = "hidden" name = "selectnames" value = "text 1"/> <br/> <input type = "checkbox" name = "selectvalues" value = "2"/> text 2 <input type = "hidden" name = "selectnames" value = "text 2 "/> <br/> <input type = "checkbox" name = "selectvalues" value = "3"/> Text 3 <input type = "hidden" name = "selectnames" value = "Text 3"/> <br/> <input type = "button" onclick = "OK () "/> <br/> </body> <br/> </ptml>

 

Modal pop-up window

In addition to boolean and integer values, returnvalue can also be a JS array to transmit a large amount of data. </P> <p> showmodaldialog is a modal window that always gets focus, but the pop-up form cannot be refreshed. In this pop-up window, operate the button. If you do not want to bring up a new window, you must add <base target = "_ Self"> between </P> and </pead> In the <p> pop-up window. <Br/>

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.