How do I use window.open and window.close together

Asked

Viewed 580 times

0

I’m hoping that when a person presses the button shift a tab opens and the previous one closes, but I’m not getting.

<script>
    function keyCode(event) {
        var x = event.keyCode;
        var y = event.keyCode;
        if (x == 16) {  // 16 is the Shift key

           window.open('https://google.com' ,  '_blank')

           Window.close();
        }
    }
</script>   

But only opens and does not close the previous.

  • By default, most browsers do not allow scripts close user-opened pages for security reasons. Note that when trying to use the function close, a message of the type Scripts may close only the windows that were opened by it. appears in the console. Only pages opened through script, with the function open, can be closed. Read more on W3C specification.

  • But I tested your script and did not open anything in Chome, IE, Rdge, Firefox and Safari.

1 answer

0


When you open the window, the focus goes to it and the method close-up() is ignored.

Put this code in the window that was opened (daughter):

 window.opener.close();

This will close the window that opened it (mother).

  • i did it but it did not work it opens the new tab and the previous one is not closed OBS: not and the window would be in Cap Aki a tab of the same window

  • Look at my commenting for more information.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.