Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 738
Ionică Bizău Avatar asked Feb 11 '14 07:02

Ionică Bizău


People also ask

Is there a way to close all windows with Ctrl-W?

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.

What is the shortcut to close all tabs in a browser?

Ctrl+W in an Internet browser. In all major Internet browsers (e.g., Chrome, Edge, Firefox, Opera), pressing Ctrl + W closes the current tab. If there aren't multiple tabs open, pressing Ctrl + W closes the browser. Firefox shortcuts. Google Chrome shortcuts.

What is the Ctrl+W shortcut?

Alternatively referred to as Control W and C-w, Ctrl+W is a shortcut key most often used to close a program, window, tab, or document. How to use the Ctrl+W shortcut key.

How to use Ctrl+W in Internet browser?

To use this keyboard shortcut, press and hold either Ctrl key, and while continuing to hold, press W. Ctrl+W in an Internet browser In all major Internet browsers (e.g., Chrome, Edge, Firefox, Opera), pressing Ctrl + W closes the current tab. If there aren't multiple tabs open, pressing Ctrl + W closes the browser.


2 Answers

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.

like image 67
Narpas Avatar answered Sep 18 '22 07:09

Narpas


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/

like image 27
guzmanfg Avatar answered Sep 18 '22 07:09

guzmanfg