javascript子窗口重定向主窗口

标签 javascript window self window.opener

main.php 有

<a href="redirect.php" target="_blank">open new window</a>

然后redirect.php有

top.top.location.href='http://xx.com/index.html';

当我点击链接打开新窗口时,redirect.php 在新窗口中打开并重定向 http://xx.com/index.html .问题在这里:在此页面中,javascript 代码强制关闭此新窗口并将主窗口重定向到 http://xx.com/index.html .

JavaScript 代码 http://xx.com/index.html有:

try{
        if(opener) {
            opener.location.href=this.location.href;
            top.close();
        }
    }

如何防止主页面被子窗口关闭?

this is live example

最佳答案

在您的代码中 (http://xx.com/index.html)

try{
    if(opener) {
        opener.location.href=this.location.href; // Line-1
        top.close(); // Line-2
    }
}

1. 第 1 行重定向打开器(父/主窗口)而不是关闭它,opener 属性返回对打开窗口的窗口的引用,因此,如果您删除 Line-1,它将不再被重定向。

2. Line-2 正在关闭当前(top 引用的子窗口本身)窗口,因此如果您删除此行,它将不再关闭.

只需删除整个 try block ,您的问题就会得到解决,我很困惑为什么您不自己删除这些行,因为您知道这些代码会导致您在问题中描述的问题。

关于javascript子窗口重定向主窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9743561/

相关文章:

python - wx.FindWindowByName() 的例子

c++ - Win32 C++将窗口位置设置为右下角

ios - UICollectionView .reloadData() 仅显示部分

php - 当使用 !(self::$_instance instanceof self) 为什么这不起作用

javascript - 覆盖 console.log|错误,winston 不再工作

c++ - OpenGL/GLFW : glfwOpenWindow hanging (barebones application)

javascript - jQuery datepicker 发布错误的日期值

Ruby:在让出过程中,谁是 block 内的 self?

javascript - 多页表单提交

javascript - facebook 聊天框持久化