3

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();
        }
    }

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

这是一个活生生的例子

4

1 回答 1

0

In your code (http://xx.com/index.html)

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

1. Line-1 is redirecting the opener (parent/main window) not closing it, the opener property returns a reference to the window that opened the window, so if you remove Line-1 it won't be redirected anymore.

2. Line-2 is closing the current (the child window itself referred by top) window, so if you remove this line it won't be closed anymore.

Just remove whole try block, your problem will be solved and I'm confused why didn't you remove those lines by yourself as because you knew those codes are responsible for the problem you described in your question.

于 2012-03-16T23:01:18.597 回答