We have a client requirement that has stumped me, embarrassingly enough.
The client has a set of links that need to open in a popup window - if you click on any of the links, it should reuse the same popup window. Easy enough, I thought, so I threw something like this together:
<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>
If you put that into an html file, all behaves as expected.
My problem is that when I use the client's intranet URLs per their requirement, the behavior I see is a开发者_C百科s follows:
Click on one of the popup links (popup link opens in a new window)
Click on another of the popup links (link replaces page opened in the first popup)
Close the popup window.
Click one of the popup links (doesn't matter which, opens in a new popup window as expected)
Click on another of the popup links (popup opens in a new popup window, not reusing the popup window as expected)
The weird thing is that if I step through the javascript code in Firebug, it correctly gets to the branch of the if statement that determines that the popup window exists and is not closed (so it should be reused), but the line of code:
popupWin.location.href = url;
Ends up opening a new window for some reason.
So, any idea what's going on? I'm guessing something bizarre on the pages that the client wants me to popup is screwing things up in some mysterious fashion, but I can't figure out what it is. Wish I could provide the links to you, but unfortunately they're private.
Thanks much for any help.
Mustafa
Isn't this functionality inherent in HTML? Shouldn't it work without the javascript?
精彩评论