Link to home
Start Free TrialLog in
Avatar of bobyk123
bobyk123

asked on

communication between two browsers

hi,

   I have a problem. I need to communicate between a parent window and the child window and viceversa. (i.e) I need to pass parameters from one window to another.
   
    I tried but was unsuccessful.

   Is it possible?

   If so can u please give me the code for that.
Avatar of searlas
searlas

Do you need this in JSP, or JavaScript?  JavaScript follows:

parent.html:
<html>
    <head>
        <script type="text/javascript">
            var child = null;
            function openChild() {
                child = window.open("child.html", "child");

            }
            function hiKid() {
                if (child && !child.closed) {
                    // DOM manipulation
                    child.document.getElementById("marker").innerHTML = 'hi kid';
                    // change value of javascript val on child
                    child['someVal'] = "parents rule";

                    // call method on child
                    child['childMethod']();
                }
            }

            // called from child frame
            function parentMethod(parentVal) {
                document.getElementById("var").innerHTML = parentVal;

            }
        </script>
    </head>
    <body onLoad="openChild();setTimeout('hiKid()', 1000)">
        Message from child: <b><span id="marker"></span></b><br/>
        Value set by child: <i><span id="var"></span></i>
    </body>
</html>


child.html
<html>
    <head>
        <script type="text/javascript">
            function helloDad() {
                if (opener != null && !opener.closed) {
                    opener.document.getElementById("marker").innerHTML = 'hi dadda';
                    // call method on parent, with parameter
                    opener['parentMethod']('pass me a parameter');
                }
            }

            var someVal = 0;
            function childMethod() {
                document.getElementById("varSpan").innerHTML = someVal;

            }
        </script>
    </head>
    <body onLoad="helloDad()">
        Message from parent: <b><span id="marker"></span></b><br/>
        Value set by parent: <i><span id="varSpan"></span></i>
    </body>
</html>

Avatar of bobyk123

ASKER

thanks for the reply. searlas your answer is working
but in addition i need to close the child window after the parameters are sent to the parent window

hope i will get the reply quickly.

In the example above, you would just change the method that the child window is calling...

function parentMethod(parentVal) {
  document.getElementById("var").innerHTML = parentVal;
  child.close(); // close child window
  child = null; // remove reference
}
can i pass array from a window to another?
if so, how can it be done
As shown above:

// change value of javascript val on child
child['someVal'] = "parents rule";

substitute an array for "parents rule";

var localArray = new Array(....);
child['someVal'] = localArray;
the array has to be passed from child window to parent window
It's the same...  replace child with opener...
opener['someVal'] = localArray;
I tried that by replacing but i am getiing error

I declared an array with 5 elements in it. and passed the array to the parent. but i am getting the following error.

  the error is :
        in parent window it is "object dossent support this property or method"
        in the child window it is "object expected"
       
ASKER CERTIFIED SOLUTION
Avatar of searlas
searlas

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks searlas  for answering ny questions

   the array values are being passed from child to parent browser

once again thanks for your support.
 
thanks searlas  for answering ny questions

   the array values are being passed from child to parent browser

once again thanks for your support.