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

查看:15
本文介绍了如何在另一个窗口中检查打开的 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天全站免登陆