最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I check for an open URL in another window? - Stack Overflow

programmeradmin2浏览0评论

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 ments 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;
    }
}

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 ments 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;
    }
}
Share Improve this question edited May 23, 2017 at 12:19 CommunityBot 11 silver badge asked Feb 9, 2009 at 16:50 Greg ReynoldsGreg Reynolds 10.2k15 gold badges52 silver badges62 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 4

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)

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.

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.

@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.');return false;">Click me first</a>, close and open this window, then
<a href="#" onclick="winClose('http://google.');return false;">click me to close the other window</a>

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

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

发布评论

评论列表(0)

  1. 暂无评论