javascript子窗口重定向主窗口

时间:2012-03-16 19:44:48

标签: 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

1 个答案:

答案 0 :(得分:0)

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

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

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

2。第2行关闭当前(子窗口本身由顶部引用)窗口,因此如果删除此行,则不会已经关闭了。

只需移除整个试用版,您的问题就会解决,我很困惑为什么您不自行删除这些行,因为您知道这些代码是您在问题中描述的问题的原因。

相关问题