0

Ive read earlier that we cannot close the current tab using jQuery or Javascript.

My question is, Is it possible to use the ASCII codes for "ctrl+w" (that is 23) and calling it in jquery/JS to close the current tab?

I tried this code:

<script>
function closethis(){
    $(document).keypress(function(e) {
        if(e.which == 23) {
            alert('Are you sure you want to close?');
        }
    });
}
</script>

but doesnt work.. any leads??

  • 1
    You're trying to send a command (shortcut) to the browser. This is not possible. Otherwise, you would be able to do other things like even close the browser itself (Alt + F4), which obviously doesn't make much sense. – marsze Dec 04 '14 at 13:41
  • yeah i know.. was just trying an alternative to close the current tab.. but none are working. I cannot use window.close() in my case as i am not opening a new window using JS..so that. –  Dec 04 '14 at 19:02

1 Answers1

0

No, you cannot close windows your script hasn't opened in any way.

(If your script opened a window with var newWindow = window.open(...), you can close it with newWindow.close().)

The most you can do in windows that you didn't open is show a dialog box when the tab is being closed by the user. For that, see How do I stop a page from unloading (navigating away) in JS? and unload on MDN.

Community
  • 1
  • 1