jQuery如何防止重新打开新窗口?

当用户按下F2键时,我有以下代码打开一个新窗口。

$(document).bind('keyup', function(e){ if(e.which==113) { window.open('newPage.htm','_blank','','false'); } }); 

现在,我想阻止用户打开一个新窗口,而前一个窗口仍然打开。 我怎么能点到它?

 var opened = null; $(document).bind('keyup', function(e){ if (e.which == 113 && (!opened || !opened.window)) opened = window.open('newPage.htm','_blank','','false'); ​});​ 

给你的窗口一个像

 var mywindow = window.open("foo.html","windowName"); 

然后你可以通过这样做检查窗口是否关闭

 if (mywindow.closed) { ... } 

你可以尝试这个..

  var windowOpened=false; $(document).bind('keyup', function(e){ if(e.which==113 && !windowOpened) { window.open('newPage.htm','_blank','','false'); windowOpened=true; } }); In the newPage.htm add code to make windowOpened=false when u close that window. < html> < head> < title>newPage.htm< /title>  < /head> < body onbeforeunload="setFalse()"> < /body> < /html> 

当窗口关闭时,window.document会发生什么? 它是否为空,也许您可​​以通过null检查它。

 var popupWindow; $(document).bind('keyup', function(e){ if(e.which==113 && (!popupWindow || !popupWindow.document)) { popupWindow = window.open('newPage.htm','_blank','','false'); } });