如何在另一个窗口中检查打开的URL?

时间:2022-07-14 21:09:02

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?

是否可以在浏览器中打开窗口,检查特定的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...

编辑:在这里(以及另一个问题)有很多有用的评论后,这是应用程序启动器的最终代码。本质上,它尝试使用适当的名称获取打开窗口的位置。如果这导致异常(由于隐私问题),则判断应用程序已加载。如果它是“about:blank”,那么它是一个新窗口。这适用于Firefox,IE7和谷歌浏览器。感觉很脏......

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

6 个解决方案

#1


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

@annakata(即使你存储它们,你也无权再关闭它们)

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:

不对。如果您具有窗口的名称,则即使关闭并重新打开了开启器,也可以使用window.open重新建立到窗口的链接。例如:

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

#2


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)

由于其他人已经提出了所有权/ cookie状态存储:这只有在您打开窗口的同一文档时才有效,即在用户关闭窗口并重新打开的情况下,这些引用确实丢失了(即使你存储它们,你将无权再关闭它们)

#3


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

在JavaScript中,您只能获得对当前窗口的引用以及使用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.

您可以检查winRef.closed以查看用户是否关闭了窗口。不过,我不确定这是否适用于所有浏览器。

#4


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.

如果为每个窗口指定了一个唯一的窗口名称(window.open的第二个参数),则再次使用相同的窗口名称调用window.open将打开窗口(如果窗口关闭),或者返回对现有窗口的引用而不打开新窗口窗口。

#5


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

你实际上可以用饼干做但是......如果你问我,你就不会这样做。

#6


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

#1


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

@annakata(即使你存储它们,你也无权再关闭它们)

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:

不对。如果您具有窗口的名称,则即使关闭并重新打开了开启器,也可以使用window.open重新建立到窗口的链接。例如:

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

#2


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)

由于其他人已经提出了所有权/ cookie状态存储:这只有在您打开窗口的同一文档时才有效,即在用户关闭窗口并重新打开的情况下,这些引用确实丢失了(即使你存储它们,你将无权再关闭它们)

#3


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

在JavaScript中,您只能获得对当前窗口的引用以及使用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.

您可以检查winRef.closed以查看用户是否关闭了窗口。不过,我不确定这是否适用于所有浏览器。

#4


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.

如果为每个窗口指定了一个唯一的窗口名称(window.open的第二个参数),则再次使用相同的窗口名称调用window.open将打开窗口(如果窗口关闭),或者返回对现有窗口的引用而不打开新窗口窗口。

#5


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

你实际上可以用饼干做但是......如果你问我,你就不会这样做。

#6


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