5

Is there a way to get a list of all the open browser windows if they are from the same domain as the window that is trying to get the list?

Johann
  • 27,536
  • 39
  • 165
  • 279

2 Answers2

9

In general, no.

Unless there is a "connection" between the windows (e.g., one window opened all the other using window.open), browser windows can't interact because of security reasons.

Edit:

If you assign a name to your window, you can regain control over it after refreshing the parent page.

  1. windowVar = window.open('somePage.html', 'windowName'); opens a child window with name windowName.

  2. After refreshing the parent page, windowVar = window.open('', 'windowName'); re-associates the variable windowVar with the window of name windowName.

  3. Now, windowVar.location.href= 'logout.html'; lets you log out your user.

Edit:

Assuming you use PHP, you could do something like this:

Create logged.php with a function logged_in that verifies if the session ID is still valid.

<?php
    if (isset($_GET['sid']))
            if (logged_in($_GET['sid']))
            echo "in";
    else
            echo "out";
?>

Include check() function in your pages.

function check()
{
    var url = "http://redtwitz.com/test/logged.php?sid=" + sessionId;
    var request;
    try
    {
        request = new XMLHttpRequest();
    }
    catch(error1)
    {
        try
        {
            request = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(error2)
        {
            request = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    request.open("GET", url, false);
    request.setRequestHeader("User-Agent",navigator.userAgent);
    request.send(null);
    if(request.status==200)
        if(request.responseText == "out")
            window.location.href = "logout.html";
}

Call check function every 5 seconds.

<body onload="setInterval(check, 5000);">
Dennis
  • 14,264
  • 2
  • 48
  • 57
  • One window actually does open another but I noticed that if I reload the parent page, the child page is no longer connected to the parent. – Johann Oct 26 '11 at 16:43
  • 1
    @AndroidDev That seems correct, because the JS window object of the caller window is a new one if you reload the page. – yunzen Oct 26 '11 at 16:44
  • So if my parent page had a sign out button that when pressed causes the parent page to reload, the child page still looks like it is signed in. Because the parent cannot communicate with the child anymore, any idea on how it might be possible to get the child to reload itself? I notice that if you do this in Gmail, the two stay in sync. Maybe Google is polling both windows to accomplish this?? – Johann Oct 26 '11 at 16:50
  • The sign out button could trigger an action on the child page before the parent page is told to reload. – James Oct 26 '11 at 17:01
  • @James: This will not work directly if the parent page is reloaded before the sign out button is pressed. – Dennis Oct 26 '11 at 17:06
  • Triggering an action on the child is something I attempted. The problem with this is that the parent needs to communicate with the child and cause the child to reload. Since the child shares the same session as the parent, the child will actually reload before the parent has. The parent needs to cause the server session to re-initialize before any child windows can reload. Perhaps one workaround is that the parent store something on the server first to indicate that it is going to do a reload. Then it notifies the child windows which reload but are blocked until the session has completed. – Johann Oct 26 '11 at 17:10
  • But even this solution is useless if the user closes the parent and leaves the child open. – Johann Oct 26 '11 at 17:11
  • @AndroidDev: I've updated my answer to address your last question. – Dennis Oct 26 '11 at 17:25
  • Your edited version won't work. You are assuming that you already know what child page is open. You don't. It could be one of many, so you cannot hardcode the page as you are showing .open('somePage.html'). Normally when you open the window, you keep a reference to it but that reference is lost when the page reloads. I have a feeling it's not possible. – Johann Oct 26 '11 at 17:27
  • To make this work, you could store the opened windows' name in a cookie. Would require cookies though... Are you **just** interested in logging out all child pages when the parent page logs out or is there anything you want to achieve? – Dennis Oct 26 '11 at 17:32
  • Just want to log out children pages or even log them in. Also, it needs to work vice versa regardless whether they are on the parent or child page. I doubt cookies would be of any value. Whenever a child or parent is informed to reload, it probably needs to have some mechanism that blocks itself until the session has been terminated on the server or re-established. I hate blocking, so it would probably have to be polled. Even that is ugly as well. – Johann Oct 26 '11 at 17:37
  • It doesn't have to be ugly. What server-side language are you using? PHP? – Dennis Oct 26 '11 at 18:32
1

Alternatively, you can implement Chrome extension and do your task by using extension api: http://code.google.com/chrome/extensions/tabs.html But it will work in Chrome browser only.

Pavel Podlipensky
  • 8,201
  • 5
  • 42
  • 53