Javascript:查找在相同域中打开的浏览器窗口 [英] Javascript: Find browser windows open with the same domain

查看:57
本文介绍了Javascript:查找在相同域中打开的浏览器窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果所有打开的浏览器窗口都与试图获取列表的窗口来自同一域,是否可以获取所有打开的浏览器窗口的列表?

Is there a way to get a list of all the open browser windows if they are from the same domain as the window that is trying to get the list?

推荐答案

通常,不会.

除非窗口之间存在连接"(例如,一个窗口使用 window.open 打开所有其他窗口),否则出于安全原因浏览器窗口将无法交互.

Unless there is a "connection" between the windows (e.g., one window opened all the other using window.open), browser windows can't interact because of security reasons.

如果您为窗口分配了名称,则刷新父页面后即可重新获得对其的控制.

If you assign a name to your window, you can regain control over it after refreshing the parent page.

  1. windowVar = window.open('somePage.html','windowName'); 打开一个名为 windowName 的子窗口.

  1. windowVar = window.open('somePage.html', 'windowName'); opens a child window with name windowName.

刷新父页面后, windowVar = window.open('','windowName'); 将变量 windowVar 与窗口重新关联名称 windowName .

After refreshing the parent page, windowVar = window.open('', 'windowName'); re-associates the variable windowVar with the window of name windowName.

现在, windowVar.location.href ='logout.html'; 可让您注销用户.

Now, windowVar.location.href= 'logout.html'; lets you log out your user.

假设您使用PHP,则可以执行以下操作:

Assuming you use PHP, you could do something like this:

使用函数 logged_in 创建 logged.php ,该函数验证会话ID是否仍然有效.

Create logged.php with a function logged_in that verifies if the session ID is still valid.

<?php
    if (isset($_GET['sid']))
            if (logged_in($_GET['sid']))
            echo "in";
    else
            echo "out";
?>

在页面中包含 check()函数.

function check()
{
    var url = "http://redtwitz.com/test/logged.php?sid=" + sessionId;
    var request;
    try
    {
        request = new XMLHttpRequest();
    }
    catch(error1)
    {
        try
        {
            request = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(error2)
        {
            request = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    request.open("GET", url, false);
    request.setRequestHeader("User-Agent",navigator.userAgent);
    request.send(null);
    if(request.status==200)
        if(request.responseText == "out")
            window.location.href = "logout.html";
}

每5秒钟进行一次呼叫检查功能.

Call check function every 5 seconds.

<body onload="setInterval(check, 5000);">

这篇关于Javascript:查找在相同域中打开的浏览器窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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