【问题标题】:javascript child window redirecting main windowjavascript子窗口重定向主窗口
【发布时间】:2012-03-16 19:44:47
【问题描述】:

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

【问题讨论】:

  • 对你的问题多加努力。这没有任何意义。
  • 好的,我提供了更多细节。

标签: javascript window self window.opener


【解决方案1】:

在您的代码中 (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. 第 2 行正在关闭当前(top 引用的子窗口本身)窗口,因此如果您删除此行,它将不再关闭.

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

【讨论】:

  • 我想,我无法正确解释我的问题。我无法更改 (xx.com/index.html) 页面。我正在从我的网站链接这个网站,我希望 (xx.com/index.html) 不要关闭我的网站。
猜你喜欢
  • 2011-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2014-04-18
  • 1970-01-01
  • 2011-04-24
相关资源
最近更新 更多