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

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

Whas 87key code. I also know what e.ctrlKeyis true, if pressed at that moment Ctrl.

Is it possible to intercept the Ctrl+ key Wand not close the tab?

I tried:

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

But it seems that in this case, the priority of the browser takes precedence, and the tab is closed.

I am looking for a solution: if this is not possible using JavaScript, perhaps a browser add-on can do this.

+4
source share
4 answers

You can avoid closing tabs using the "beforeunload" event

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

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

+1

:

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

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

    return false;
});
0

, , . , , , . . !


, javascript -, . : , - . - , - . . , , .

- -. " ". beforeunload , , "" "". , , , . beforeunload , .

( ), , preunload, - -, , - . - , , -, , . , - !

beforeunload - , , - , , , , .

0

, , , , , CTRL + SHIFT + W ( ) , CTRL-W CTRL-TAB . shift, Chrome , CTRL-W.

So, for Windows (not sure what you mean Linux, but you can do the same with xbindkeys):

(Apologies: if the format "code" does not display the following:

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

I hope this will be useful for those who are looking for a way to stop Chrome, even after receiving the CTRL-SHIFT-W key combination.

0
source

Source: https://habr.com/ru/post/1526299/


All Articles