Greg Reynolds
Greg Reynolds

Reputation: 10186

How can I check for an open URL in another window?

This is a follow up to my last question Open a window if the window does not already exist Essentially, I am now keeping a list of all the window references that have been opened by a page, and only allowing them to be opened if they are not already open. Then a potential problem struck me - it is of course possible for a user to shut down the original window, and open it again, thus losing the list of window references.

Is it possible to loop through the windows open in a browser, checking for a particular URL?

Edit: After a lot of helpful comments here (and on the other question), here is the final code for the application launcher. Essentially, it tries to get the location of the open window with the appropriate name. If that causes an exception (because of a privacy issue), then the application is judged to have been loaded. If it is "about:blank", then it is a new window. This works on Firefox, IE7 and Google Chrome. It feels dirty...

var g_urlarray = [];

Array.prototype.has = function(value) {
    var i;
    for (var i in this) {
        if (i === value) {
            return true;
        }
    }
    return false;
};


function launchApplication(l_url, l_windowName)
{
    var l_width = screen.availWidth;
    var l_height = screen.availHeight;
    var winRef;

    var l_params = 'status=1' +
        ',resizable=1' +
        ',scrollbars=1' +
        ',width=' + l_width +
        ',height=' + l_height +
        ',left=0' +
        ',top=0';
    if (g_urlarray.has(l_url)) {
        winRef = g_urlarray[l_url];
    }
    if (winRef == null || winRef.closed) {
        winRef = window.open('', l_windowName, l_params);
        var l_openNew = 0;
        try {
            if (winRef.location == 'about:blank') {
                l_openNew = 1;
            }
        }
        catch (e) {
            l_openNew = 0;
        }
        if (l_openNew === 1)
        {
            winRef.location = l_url;
            winRef.moveTo(0,0);
            winRef.resizeTo(l_width, l_height);
        }
        g_urlarray[l_url] = winRef;
    }
}

Upvotes: 5

Views: 17865

Answers (6)

adam0101
adam0101

Reputation: 30995

@annakata (and even if you stored them, you wouldn't have permission to close them any more)

Not true. If you have the name of the window, you can use window.open to reestablish a link to the window even if the opener was closed and reopened. For example:

<script>
function winOpen(url){
  return window.open(url,getWinName(url));
}
function winClose(url){
  var win = window.open("",getWinName(url));
  win.close();
}
function getWinName(url){
  return "win" + url.replace(/[^A-Za-z0-9\-\_]*/g,"");
}
</script>
<a href="#" onclick="winOpen('http://google.com');return false;">Click me first</a>, close and open this window, then
<a href="#" onclick="winClose('http://google.com');return false;">click me to close the other window</a>

Upvotes: 0

Josh Stodola
Josh Stodola

Reputation: 82483

Setup an array, and increment it with window references when you open them...

var wins = new Array();

function openWindow(url) {
  wins.push(window.open(url));
}

Then when you wish to check the status of the windows, you can loop through them like this, and remove the windows that are not opened...

function updateWindowArray() {
  for(var i = 0, l = wins.length; i < l; i++) {
    if(wins[i] == null || wins[i].closed)
      arrayRemove(wins, i, i + 1);
  }
}

function arrayRemove(array, from, to) {
  var rest = array.slice((to || from) + 1 || array.length);
  array.length = from < 0 ? array.length + from : from;
  return array.push.apply(array, rest);
}

Best regards...

Upvotes: 0

annakata
annakata

Reputation: 75794

No, this would be a security/privacy issue.


Since others have brought up the ownership/cookie state storage: this only works if you are also the same document which opened the window, i.e. in the scenario where the user shuts the window down and reopens then these references are indeed lost (and even if you stored them, you wouldn't have permission to close them any more)

Upvotes: 4

Tamas Czinege
Tamas Czinege

Reputation: 121294

You could actually do it with cookies but... if you ask me, you won't do it.

Upvotes: 0

adam0101
adam0101

Reputation:

If you gave each window a unique window name (the second argument of window.open), calling window.open again with the same window name will either open the window if it's closed, or return a reference to the existing window without opening a new window.

Upvotes: 2

EndangeredMassa
EndangeredMassa

Reputation: 17528

In JavaScript, you can only gain references to the current window and any windows that you open with window.open.

You could check for winRef.closed to see if the user closed the window, though. I'm not sure if this works well on all browsers or not, though.

Upvotes: 2

Related Questions