How can jQuery prevent a new window from reopening?

I have the code below to open a new window whenever the user presses the F2 key.

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

Now I want the user to not open a new window while the previous one is not yet open. How can I calculate it?

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

You can try this.

    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> 
0
source

Give your window a name like

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

Then you can check if the window is closed by doing

if (mywindow.closed) { ... }
0
source

When windows are closed, what happens with window.document? Whether it has become null, perhaps you can check it with null.

var popupWindow;

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

All Articles