Javascript 尝试重用弹出窗口时的奇怪行为

Javascript 尝试重用弹出窗口时的奇怪行为,javascript,popup,Javascript,Popup,我们有一个客户的要求,这让我很难堪 客户端有一组需要在弹出窗口中打开的链接-如果您单击任何链接,它应该重用相同的弹出窗口。很简单,我想,所以我把这样的东西放在一起: <html> <head> <title>Test</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type="text/java

我们有一个客户的要求,这让我很难堪

客户端有一组需要在弹出窗口中打开的链接-如果您单击任何链接,它应该重用相同的弹出窗口。很简单,我想,所以我把这样的东西放在一起:

<html> <head> <title>Test</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type="text/javascript"> var popupWin = null; function openWindow(url) { if (popupWin == null) { popupWin = window.open(url,'p2p',''); } else { if (!popupWin.closed) { popupWin.location.href = url; } else { popupWin = window.open(url,'p2p',''); } } popupWin.focus(); } </script> </head> <body marginheight="0" marginwidth="0" leftmargin="0" topmargin="0" bgcolor="#ffffff"> <ul> <li><a href="http://www.google.com" target="p2p" onclick="openWindow(this.href);return false;">Google</a></li> <li><a href="http://www.facebook.com" target="p2p" onclick="openWindow(this.href);return false;">FB</a></li> <li><a href="http://www.apple.com" target="p2p" onclick="openWindow(this.href);return false;">Apple</a></li> <li><a href="http://www.espn.com" target="p2p" onclick="openWindow(this.href);return false;">ESPN</a></li> </ul> </body> </html> 测验 var popupWin=null; 函数openWindow(url){ if(popupWin==null){ popupWin=window.open(url,'p2p',''; }否则{ 如果(!popupWin.closed){ popupWin.location.href=url; }否则{ popupWin=window.open(url,'p2p',''; } } popupWin.focus(); }
如果您将其放入一个html文件中,所有操作都会按预期进行

我的问题是,当我按照客户机的要求使用其intranet URL时,我看到的行为如下:

  • 单击其中一个弹出链接(弹出链接在新窗口中打开)

  • 单击另一个弹出链接(链接替换在第一个弹出窗口中打开的页面)

  • 关闭弹出窗口

  • 单击其中一个弹出链接(无论是哪一个,在新的弹出窗口中打开,如下所示) 预期的)

  • 单击另一个弹出链接(弹出窗口在新的弹出窗口中打开,而不是按预期重复使用弹出窗口)

  • 奇怪的是,如果我在Firebug中单步执行javascript代码,它会正确地到达if语句的分支,该分支确定弹出窗口存在且未关闭(因此应该重用),但代码行:

    popupWin.location.href = url; popupWin.location.href=url; 由于某种原因,最终打开了一个新窗口

    知道发生了什么吗?我猜客户希望我弹出的页面上有些奇怪的东西以某种神秘的方式把事情搞砸了,但我不知道是什么。希望我能提供给你的链接,但不幸的是它们是私人的

    谢谢你的帮助


    Mustafa

    这不是HTML固有的功能吗?如果没有javascript,它不应该工作吗?

    在页面中添加一个测试链接可能会很有趣。该链接将运行一个脚本来显示几个值为popuwwin、popuwwin.location.href和popuwwin.closed的警报框。单击常规链接之前和之后的测试链接,弹出窗口打开,关闭后。也许会有线索。是的,我刚测试过。删除javascript,它应该可以正常工作。