Javascript: Child windows talking to each other???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newsteve1
    New Member
    • Aug 2007
    • 12

    #1

    Javascript: Child windows talking to each other???

    Hi,

    Im trying to design a fun page where a after an onclick on the parent page, several small child windows are opened. Upon clicking the content of any of those windows, I want all of the child windows to close. I can get self.close() to close the clicked window, but I cannot communicate with the other child windows, even if I reference them by their name, ie winName.close() ... are there special rules for doing this?

    A miniaturized version of what I am trying to do can be seen here:

    (click the red square!)

    Thanks for any insight you may have...
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Steve.

    Try setting a variable to hold the new window:
    [code=javascript]
    var newWindow1 = window.open( ... );
    .
    .
    .
    newWindow1.clos e();
    [/code]

    Note that you'll probably want to take care of this stuff in the main window. In each child window, call this:
    [code=javascript]
    window.opener.c loseAllChildren ();
    [/code]

    to do something like this:
    [code=javascript]
    var childWindows = [];
    function openChild()
    {
    childWindows[childWindows.le ngth] = window.open( ... );
    }

    function closeAllChildre n()
    {
    for(var i = 0; i < childWindows.le ngth; ++i)
    {
    childWindows[i].close();
    }
    childWindows = [];
    }
    [/code]

    Comment

    • newsteve1
      New Member
      • Aug 2007
      • 12

      #3
      Hi,

      Its not working for me...
      I assume I should be filling var childWindows with the names of my child windows? Or the URLs?

      Are you sure this can be done? It would be great if it worked but I was just reading that child windows quite simply cant talk to each other...?

      Cheers

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Steve.

        Note this line:
        [code=javascript]
        childWindows[childWindows.le ngth] = window.open( ... );
        [/code]

        What you are doing here is literally storing a reference to the window that you opened in one of the elements of the childWindows array.

        This is different from, say, PHP where you store the name of a variable or function.

        To use a simpler example:
        [code=javascript]
        var newWin = window.open( ... );
        .
        .
        .
        newWin.close();
        [/code]

        In the example above, newWin is literally an alias for the newly-opened window. Similarly, you can do these as well:
        [code=javascript]
        newWin.location .href = 'http://thescripts.com' ;
        window.location .href = 'http://thescripts.com' ;
        [/code]

        Comment

        Working...