Ionică Bizău
Ionică Bizău

Reputation: 113455

Is it possible to catch CTRL+W shortcut and prevent tab closing?

$(window).on("keydown", function (e) {
    alert(e.keyCode);
});

W has 87 key code. I also know that e.ctrlKey is true if Ctrl is pressed in that moment.

Is possible to catch the Ctrl + W key press and prevent tab closing?

I tried:

$(window).on("keydown", function (e) {
    if (e.keyCode === 87 && e.ctrlKey) {
        return false;
    }
});

But it seems that browser shortcut has priority in this case and the tab is closed.

I am searching for a solution: if it's not possible via JavaScript, maybe a browser add-on can do this.

Upvotes: 21

Views: 13201

Answers (5)

Wayne
Wayne

Reputation: 21

Especially in firefox it's annoying, that CTRL+W can't really be turned off. Even if you plan it solely for your personal use, where changes in your config or your settings could possibly do the job.

However, you can use as a workaround the "beforeunload" event in javascript. Make sure to have the third parameter (useCapture) set to true, in order to intercept the keystrokes before they are picked up by anything else.

Here's the code:

addEventListener(
    'beforeunload',
    function(e){
        e.stopPropagation();e.preventDefault();return false;
    },
    true
);

Upvotes: 2

Malcolm Boekhoff
Malcolm Boekhoff

Reputation: 1092

I know it's probably irrelevant for your scenario, but I found the CTRL+SHIFT+W shortcut (close all windows) extremely annoying when you are just closing a load of tabs with CTRL-W and using CTRL-TAB to move between them. You sometimes accidentally hold down the shift key and then Chrome exits when you just meant to do CTRL-W.

So for Windows (not sure if you meant Linux, but you can do the same thing there with xbindkeys):

  1. Install https://www.autohotkey.com
  2. To disable the ^+W short cut on windows, add this into AutoHotkey.ahk:

(Apologies: if the following doesn't show up in "code" format)

^+w::
SetTitleMatchMode RegEx
IfWinExist, Google Chrome$
{
    WinActivate
}
return

Hopefully this will be useful for someone looking for a way to stop Chrome even getting the CTRL-SHIFT-W key combo.

Upvotes: 0

Narpas
Narpas

Reputation: 281

This is a response not to the original asker, but to anyone who finds this question via search. This post is designed to give you the information and arguments you need to return to your boss or your client and tell them why this design is not able to be realized. The beforeunload solution is provided by the top answer to this question. Good luck getting them on-board!


The design in this project stipulates that some javascript code should prevent the user from closing a web page, without providing the user with a dialog box. There is one key problem with this: this is something which malicious websites also try to do. Web browser developers work to prevent malicious websites from taking over the user experience. For that reason, there is no good way to do this. Even if we find a solution, that solution will not be future-proof, and will break without warning coinciding with a browser update.

Web browser developers provide exactly one solution to us for preventing the close of the websites. That solution is called "the beforeunload solution". The beforeunload solution will give the user a dialog box, and the text in that dialog box will have standardized, boilerplate text with a "stay" button and a "leave" button. We can add some text to that dialog box, but we cannot remove the boilerplate text and we cannot change, move or otherwise manipulate the buttons. The beforeunload solution is inflexible, but it is future-proof.

Using a non-standard solution (otherwise known as a hack) to circumvent what beforeunload provides will thrust us into the arms-race between malicious websites and the web browser developers, and we will not be able to guarantee that our website's features will work consistently from day-to-day. Malicious websites often find new tricks and hacks to keep intrusive advertisements on-screen, so even if we find something which is allowed one day it might be banned the next. The internet is littered with no-longer-working non-standard solutions which web browser developers have shut down!

The beforeunload solution is the only workable solution in the long-term, and we will need to accept that the end-user can close our website for any reason, using any standard keyboard shortcut, and the only way we can prevent this is a dialog box which requests that they stay on the site.

Upvotes: 14

guzmanfg
guzmanfg

Reputation: 79

You can avoid tab closing using 'beforeunload' event

$(window).on('beforeunload', function(e) {
    if(hasUnsaved()) {
        return 'You have unsaved stuff. Are you sure you want to leave?';
    }
});

See: http://hackab.it/2013/05/page-closing-confirm/

Upvotes: 5

Rakesh Kumar
Rakesh Kumar

Reputation: 2853

Try this code:

$(window).keypress(function(event) {
            event.preventDefault();

            if (event.which == 119 && event.ctrlKey) {
                alert("Ctrl-W pressed");
            }    

    return false;
});

Upvotes: 0

Related Questions