views:

1189

answers:

6

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;
    }
}
+4  A: 

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)

annakata
Your comment actually put me on the path to the solution - if trying to get the location of the window throws an exception, then it must have been loaded (because about:blank does not throw an exception). Many thanks.
Greg Reynolds
That's a really nice way to work past it
annakata
+2  A: 

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.

EndangeredMassa
+2  A: 

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.

A: 

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

DrJokepu
A: 

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...

Josh Stodola
A: 

@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>
adam0101
This isn't working for me in FF, Chrome, Opera or IE - is it just me?
annakata
I couldn't tell you. It worked for me in IE.
adam0101