Getting an open window's handle from its name

Discussion in 'Javascript' started by NeverLift, Aug 6, 2004.

  1. NeverLift

    NeverLift Guest

    But, if it's not open, I don't want to open it . . . using
    window.open will open it if it doesn't exist, even if the url in that
    open is null (the window is then empty -- but it's open).

    The situation is: A main window opens child windows one at a time as
    the user requests, each with its own name. The user may click in any
    child window or the main window to open a summary window, which has a
    name all windows know. I want to get that summary window's handle in
    any child window, no matter which window first opened it, so the child
    window can run a script in the summary to update its display, should
    the user have performed a selection in that child window -- but I do
    not want to open it if the user hasn't explicitly done so yet. (All
    of this is client-side. The user selections are being accumulated in
    the cookie. The 2,000 byte limit is not of concern.)

    I know I can have the summary self-update if it's open and the user
    merely shifts the focus to it. But it might be visible, even if it's
    not the focus, and I'd like to keep it up-to-date on the fly.

    I hope that's clear. Feel free to email me directly if not.

    Thanks.
     
    NeverLift, Aug 6, 2004
    #1
    1. Advertising

  2. NeverLift

    Erwin Moller Guest

    NeverLift wrote:

    > But, if it's not open, I don't want to open it . . . using
    > window.open will open it if it doesn't exist, even if the url in that
    > open is null (the window is then empty -- but it's open).
    >
    > The situation is: A main window opens child windows one at a time as
    > the user requests, each with its own name. The user may click in any
    > child window or the main window to open a summary window, which has a
    > name all windows know. I want to get that summary window's handle in
    > any child window, no matter which window first opened it, so the child
    > window can run a script in the summary to update its display, should
    > the user have performed a selection in that child window -- but I do
    > not want to open it if the user hasn't explicitly done so yet. (All
    > of this is client-side. The user selections are being accumulated in
    > the cookie. The 2,000 byte limit is not of concern.)
    >
    > I know I can have the summary self-update if it's open and the user
    > merely shifts the focus to it. But it might be visible, even if it's
    > not the focus, and I'd like to keep it up-to-date on the fly.
    >
    > I hope that's clear. Feel free to email me directly if not.
    >
    > Thanks.


    Hi,

    I think the easiest way to accomplish that is by keeping the reference to
    the summarywindow in the mainwindow.

    every childwindow that wants a handle to your summarywindow requests that by
    asking their opener.
    something like this

    mainwindow
    -----------------------------------
    <html>
    <body>

    <a href="test3.html" target="myNewWindow1">open new window 1</a><br>
    <a href="test3.html" target="myNewWindow2">open new window 2</a><br>
    <a href="test3.html" target="myNewWindow3">open new window 3</a><br>
    <hr>
    <form>
    Click <input type="button" value="here" onClick="makeSummaryWindow()"> to
    open summarywindow.
    </form>
    <hr>
    <script type="text/javascript">
    var summaryWindow;
    function makeSummaryWindow(){
    summaryWindow = window.open("","mySummaryWindow");
    summaryWindow.document.open();
    summaryWindow.document.write("summary");
    summaryWindow.document.close();
    }

    function getSummaryWindowHandle(){
    return summaryWindow;
    }
    </script>

    </body>
    </html>

    -----------------------------------


    And test3.html:
    -----------------------------------

    <html>
    <body>
    I am childwindow
    <br>
    <script type="text/javascript">
    var myOpener = opener;
    var theSummaryWindowHandle = opener.getSummaryWindowHandle();
    alert("myOpener="+myOpener);
    alert("theSummaryWindowHandle="+theSummaryWindowHandle);
    </script>
    </body>
    </html>


    -----------------------------------

    Of course you should handle the possible non-existing summaryWindow more
    nicely than I did.

    Regards,
    Erwin Moller
     
    Erwin Moller, Aug 6, 2004
    #2
    1. Advertising

  3. Been working on the issue, came up with something similar.

    Your solution works only if the main window is the only opener of the
    summary. In my case, any of the child windows may have initially asked
    for the summary window . . . and even that child window is likely to be
    closed as the user looks at additional child windows, each being
    selected in the main window.

    What I came up with is: A script in the main window that does the
    opening of the summary, It might be triggered there, of course. But
    the action item in a child window that is to open the summary does so by
    asking its opener -- the main window -- to do so, by running that
    function in the opener. It also updates the summary by running a
    function in the opener, and that function can check to see if it ever
    opened the summary and, if not, not doing the update to it. So, all
    window handles reside in the main window, all actions for the secondary
    windows (the summary window and any of its ilk) funnel through the main
    window.

    Now, of course, if the user closes the main window, or even goes to a
    different page in it . . . no child window requests to open or update
    the summary will work. hmmm.

    It would be so much easier if the child windows could find the summary,
    if it is open, on their own, no matter how it got opened, without having
    to run to momma -- momma may be gone.

    I don't think the window handle can be stuffed into the cookie, else I'd
    just have each summary request check there. Or can it? My cookie use
    so far is pretty basic -- chunks of text, not object references. Can a
    cookie hold an object like the window handle?

    {aka NeverLift}

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Gary Marquart, Aug 6, 2004
    #3
  4. NeverLift

    Robert Guest

    (NeverLift) wrote in message news:<>...
    > But, if it's not open, I don't want to open it . . . using
    > window.open will open it if it doesn't exist, even if the url in that
    > open is null (the window is then empty -- but it's open).
    >


    This may help...

    Is your window that contains the list of open windows a popup? I
    assumed that it was, but you could do the same for a frame.

    You could track the status of your windows in some variables.

    I used the onunload to initiate the tracking of a closed window. I
    have heard that onunload was unrelaiable, but it seems tp work for me.
    I put an alert at the end of resetVar and observed that resetVar was
    invoked and the window closed before I hit enter. This is a good
    sign because it shows can get control to do some reseting of control
    states, but you are not able to block the window from exiting.

    I put together three html files to show how you could track the status
    of a popup window. Run main.html. You will see alert messages that
    step you through the running.

    I tried this on Netscape 7.1 and IE 5.2 under MacOS 10.2.6.


    main.html

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE>Check before opening:main.html</TITLE>

    <SCRIPT type="text/javascript">

    function WinObj()
    {
    this.openned = false;
    this.running = false;
    this.window = "";
    }

    var winList = [ ];

    winList["winFirst"] = new WinObj();
    winList["winSecond"] = new WinObj();

    function setVar(name)
    {
    winList[name].running = true;
    }

    function resetVar(name)
    {
    winList[name].openned = false;
    winList[name].running = false;
    }

    function foo(name,htmlFile)
    {
    var theData, newWindow, URLstring, pass;

    theData = "?none=none";
    URLstring = htmlFile + theData;
    pass = "statusbar,menubar,resizable,toolbar," +
    "height=300,width=500,location";
    winList[name].window = window.open(URLstring,name,pass);

    winList[name].openned = true;
    winList[name].window.focus();
    }

    var maxCheck = 0; // Don't run forever

    function checkForOpen()
    {
    if (winList["winFirst"].running == true)
    {
    foo("winSecond","winSecond.html");
    }
    else if (maxCheck++ < 30)
    {
    setTimeout("checkForOpen()",1000);
    }
    else
    {
    alert("setTmeout for checkForOpen has ended");
    }

    }
    </script>

    </HEAD>

    <BODY onload='
    alert("Try to open a new window next.");
    foo("winFirst","winFirst.html");
    setTimeout("checkForOpen()",1000);'>

    <p>Lets open two windows.</p>

    </BODY>

    </HTML>


    winFirst.html

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE>Little html window: winFirst.html</TITLE>
    </HEAD>
    <BODY
    onload='alert("Onload in winFirst.html, " +
    "press enter within thirty seconds to open " +
    "winSecond.html.");
    opener.setVar("winFirst");'

    onunload='opener.resetVar("winFirst");'>
    <p>I am a very small html file,
    but I hope to grow up someday.</p>
    </HTML>


    winSecond.html

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <HTML>

    <SCRIPT type="text/javascript">
    function startUp()
    {
    alert("From winSecond.html, " +
    "focus should now go to winFirst.html " +
    "after calling setVar. " +
    "( Should you close the winFirst.html window. " +
    "You will get another alert. " +
    "To see both paths, you may press enter, " +
    "then close winFirst.html then reload this one. )");

    opener.setVar("winSecond");
    if (opener.winList["winFirst"].openned == true)
    {
    opener.winList["winFirst"].window.focus();
    }
    else
    {
    alert("Problems today. Where is winFirst.html?");
    }
    }
    </script>
    <HEAD>
    <TITLE>Little window too: winSecond.html</TITLE>

    </HEAD>
    <BODY onload='startUp();'
    onunload='opener.resetVar("winSecond");'>
    <p>I am too a small html file,
    but I hope to grow bigger someday too.</p>
    <p>Most programs seem to go that way, amigo.
    Not me. I grow in complexity, but stayed small.</p>
    </HTML>


    Robert
     
    Robert, Aug 6, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RJN
    Replies:
    1
    Views:
    8,747
    Patrik Löwendahl [C# MVP]
    Oct 10, 2004
  2. RJN
    Replies:
    0
    Views:
    4,137
  3. thunk
    Replies:
    1
    Views:
    315
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    488
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    627
    thunk
    Apr 3, 2010
Loading...

Share This Page