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

时间:2012-02-20 08:56:11

标签: jquery window.open

每当用户按下F2键时,我都会在下面打开一个新窗口。

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

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

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

你可以试试这个..

    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>
<script>
function setFalse()
{
  opener.windowOpened = false;
  self.close();
  return false;
}
</script>
< /head>
< body onbeforeunload="setFalse()">
< /body> 
< /html> 

答案 2 :(得分:0)

为您的窗口命名,如

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

然后你可以通过

检查窗口是否关闭
if (mywindow.closed) { ... }

答案 3 :(得分:0)

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

var popupWindow;

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