Search code examples
javascriptstruts

How to pass value from child page to parent page with javascript in Struts


When click boo button, open showmodeldialoge and check some row in showmodeldialoge when press foo button can not get value from child page and pass to parent page parent page code:

function explain() {
//newwin = window.open('domain.do?method=showBranchForm','newwin');

var a='domain.do?method=showBranchForm';
window.showModalDialog(a, self, 'status:no;resizable:yes;help:no;scroll:yes;width:1300;height:600');

}

<input <%=branchEnable%> class="btn"
style="width: 120px" value="boo" type="button"
onclick="explain();" />

child value should be shown here:

<select id="listbranch"
multiple="multiple" size="8" name="listbranch"
style="width: 200px; font-family: tahoma; font-size: 16"/>

here is the child page:

<script language="javascript" type="text/javascript">
    function TransferData() 
    {
       opener.window.document.forms[0].elements['listbranch'].length=0;
       counter=0;
        document.forms.newform.elements
        for(i=0; i<document.forms[0].elements.length; i++)
        {
        if(document.forms[0].elements[i].type=="checkbox")
        {
            if(document.forms[0].elements[i].checked==true)
        {
            opener.window.addOption(document.forms[0].elements[i].value,document.forms[0].elements[i].id,counter); 
            counter++;
        }
        }
  }
 window.close();
}
</script>

 <tbody>
<%
    if(branchsIterator != null){
        while(branchsIterator.hasNext()){
        Branch t = branchsIterator.next();
%>
    <tr>
        <td><input type="checkbox"  id="<%=t.getBranchCode()%>"  value="<%=t.getBranchName() %>"/></td>
        <td ><%=t.getBranchName() %></td>
        <td><%=t.getBranchCode() %></td>
    </tr>
    <%} 
    }
    %>
</tbody>



<input class="btn" type="button" value="اfoo" style="width:200px" onclick="TransferData();" />

Solution

  • To get the value you should use var variable returned when you use window.showModalDialog. This is an object where you can store your value when you press a button in child window.

    window.returnValue = 'some value';
    

    So, in the parent window you can get the value after the function returns.

    var dialog = window.showModalDialog(a, self, 'status:no;resizable:yes;help:no;scroll:yes;width:1300;height:600');
    alert(dialog.returnValue);