如何在另一个窗口中检查打开的URL? [英] How can I check for an open URL in another window?

查看:85
本文介绍了如何在另一个窗口中检查打开的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我最后一个问题的后续操作

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.

是否可以在浏览器中打开的窗口循环浏览,以检查特定的URL?

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

在这里(以及另一个问题)进行了很多有用的评论之后,这是应用程序启动器的最终代码.本质上,它尝试使用适当的名称获取打开的窗口的位置.如果这导致异常(由于隐私问题),则将应用程序判断为已加载.如果它是"about:blank",则它是一个新窗口.这适用于Firefox,IE7和Google Chrome.感觉很脏...

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

推荐答案

@annakata(即使您存储了它们,也无权再将其关闭)

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

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

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>

这篇关于如何在另一个窗口中检查打开的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆