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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/528941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:56:29  来源:igfitidea点击:

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

javascriptwindowspopup

提问by Greg Reynolds

This is a follow up to my last question Open a window if the window does not already existEssentially, 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 和 Google Chrome。感觉很脏...

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

采纳答案by adam0101

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

回答by annakata

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 状态存储:这仅在您也是打开窗口的同一文档时才有效,即在用户关闭窗口并重新打开的情况下,这些引用确实丢失了(即使您存储了它们,您将无权再关闭它们)

回答by EndangeredMassa

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.closedto see if the user closed the window, though. I'm not sure if this works well on all browsers or not, though.

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

回答by EndangeredMassa

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 将在窗口关闭时打开该窗口,或者返回对现有窗口的引用而不打开一个新窗口窗户。

回答by Tamas Czinege

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

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

回答by Josh Stodola

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

此致...