Solved

parameter passing from child window to parent window

Posted on 2004-04-02
14
24,270 Views
Last Modified: 2008-09-18
hi,

   my requirment is : I need to click a button on the parent html page. this should open a new child window which has two text fields and two buttons "Ok" and "Cancel".

when i click "Ok" the values in the textfields should be passed to the parent window and stored in two variables.

when i click "Cancel"  the window should be closed and the values inthe textfield if any should not be passed

  If any one has the idea please post the code.

waiting for reply.
0
Comment
Question by:bharat_ko
  • 4
  • 4
  • 4
  • +2
14 Comments
 
LVL 6

Expert Comment

by:jkna_gunn
Comment Utility

<input type="button" name="ok" value="Ok" onclick="passValues()">
<input type="button" name="cancel" value="cancel" onClick="window.close();">

<script language="JavaScript">
function passValues()
{
    // pass variable back to parent window
    window.opener.document.yourformname.yourinputfield = document.thischildform.textfield1; // adjust to your needs
 
}
</script>

you will need inputs set up on parent page to pass values back to in above example.
0
 
LVL 6

Expert Comment

by:jarasa
Comment Utility
You should close the window after clicking ok too

<input type="button" name="ok" value="Ok" onclick="passValues();window.close();">

Javier
0
 
LVL 6

Expert Comment

by:jkna_gunn
Comment Utility
true, but he never asked for that ;)
0
 
LVL 6

Expert Comment

by:jarasa
Comment Utility
I guess so :c)
0
 

Author Comment

by:bharat_ko
Comment Utility
hi jkna_gunn,

 i could not get u with the statment "you will need inputs set up on parent page to pass values back to in above example.".

 can u explain what you mean saying "input setup".

0
 
LVL 6

Expert Comment

by:jkna_gunn
Comment Utility
sorry what i meant was that inorder to pass information back you will need somewhere to pass them back to
for example an input field (most likely).

so in my code i had this
window.opener.document.yourformname.yourinputfield

so that means on your parent page, you would need a form called yourformname that had an input field called yourinputfield. of course im sure you have these already, so all you need to do is change the names to match your code.
0
 

Author Comment

by:bharat_ko
Comment Utility
hi jkna_gunn and jkna_gunn,

     can u clearly explain what is "inputs set up on parent page" .

  I think i need to stop execution of next statments on parent window till the child window is closed.


0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:bharat_ko
Comment Utility
hi jkna_gunn and jarasa,

     can u clearly explain what is "inputs set up on parent page" .

  I think i need to stop execution of next statments on parent window till the child window is closed.


0
 
LVL 6

Expert Comment

by:jarasa
Comment Utility
Main page -> You have a Form on this page
<Form name=myForm>
<input type=text name=myField1 value="">
</Form>

Child page -> You change the value of a field inside the main page Form
window.opener.document.myForm.myField1.value = "Hello I write this from the Child Window";

You see now how it works??

Javier

0
 
LVL 6

Expert Comment

by:jarasa
Comment Utility
Don't accept my answer, OK? I'm just explaining you jkna_gunn Solution wich it the correct one.

Javier
0
 

Accepted Solution

by:
mgeiser earned 125 total points
Comment Utility

try this...


=============================================
Parent Page

function openWindow() {

      //open dialog to get the name of the Template copy
      var url = escape(appRoot + "/activityTemplateModify.do?action=SelectTemplateCopyName");
      var features = "dialogWidth:400px;dialogHeight:225px;scroll:no;status:no;resizable:no;help:no;";

      var inputArgs = new Object();
      inputArgs.templateName = selectedTemplateName;
      inputArgs.templateId = selectedTemplateId;

      var retVal2 = window.window.showModalDialog(url, inputArgs, features);
      if (retVal2 != null) {
             if (retVal2.status == 1) {
                       alert(retVal2.text1)
                       alert(retVal2.text2)
         }
        }
}

=============================================
Cihild page

buttons:
<table class="dialogLayout" id="idCommand" cellpadding="0px" cellspacing="0px" width="100%">
<tr>
<td align="right">
      <input type="button" class="commonButtonH19W54" value="OK" onclick="closeWindow(true);" tabindex="2"/>
      <input type="button" class="commonButtonH19W54" value="Cancel" onclick="closeWindow(false);" tabindex="3"/>
</td>
</tr>
</table>


    function closeWindow(accept) {
        if (accept == true) {
      ret.status = 1;
      ret.text1 = document.getElementById("text1").value;
      ret.text2 = document.getElementById("text2").value;
      window.returnValue = ret
      window.close();
        }
        else {
                //the cancel button was clicked
      ret.status = 0;
      window.close();
        }
    }
0
 

Author Comment

by:bharat_ko
Comment Utility
thanks for your replies.

 i am getting a script error.

   it says "'ret' is undefined". so i declared 'ret' as

                     var ret = new Object();  in the child window (i.e) modal dialog.

but still i am getting the same error.

can u tell me why is it so.

one more thing. can i get the reference of my parent window variable or object to the child window (i.e) modal dialog. if  it can be done , can you write the code for that



0
 
LVL 6

Expert Comment

by:jkna_gunn
Comment Utility
// parent page
<script language="JavaScript">
  function openChild() {
       var win = window.open('child.html','Child','width=400,height=275,toolbar=yes,scrollbars=yes,resizeable=yes,menubar=yes,location=yes,status=yes');
   }
 </script>
 <form name="parentForm">
 <input type="text" name="textField1" value="r">
 <input type="text" name="textField2" value="r"><br />
 <input type="button" name="openchild" value="Open Child" onClick="openChild()">
 </form>


// child page
<script language="JavaScript">
  function passValues()
  {
      window.opener.document.parentForm.textField1.value = document.childForm.textField1.value;
      window.opener.document.parentForm.textField2.value = document.childForm.textField2.value;
  }
</script>
<form name="childForm">
<input type="text" name="textField1" value="">
<input type="text" name="textField2" value=""><br />
<input type="button" name="ok" value="OK" onClick="passValues();">
<input type="button" name="cancel" value="Cancel" onClick="window.close();">
</form>
0
 

Expert Comment

by:aotyehel
Comment Utility
I have tried to alter this last piece of code using radio buttons...but it seems to ignore whether or not the radio buttons are checked or not, so does not pass the values....or rather returns a value of 'undefined'.

As I am not a Javascript whizz....could someone alter the above code to enable a child window to pass through the value of a checked radio button value to one textfield.

This would greatly be appreciated.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now