javascript子窗口重定向主窗口

javascript child window redirecting main window

本文关键字:窗口 重定向 javascript      更新时间:2023-09-26

main.php有

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

然后重定向.php有

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

当我点击链接打开新窗口,重定向.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();
        }
    }

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

这是

的实际示例

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

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

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

2.第2行正在关闭当前窗口(顶部引用的子窗口本身),因此如果删除该行,它将不再关闭。

只要删除整个try块,你的问题就会得到解决,我很困惑你为什么不自己删除这些行,因为你知道这些代码是你在问题中描述的问题的原因。