Disable ALT+F4, yes I know it isn’t recommended

Tags:



I need a JavaScript script that would disable users from closing using ALT+F4. I have looked everywhere but it just everyone just says it isn’t advised.

May not even be possible, if it isn’t I will just have to detect when the user does quit out this way and log it in a database.

Here is my current script, it detects if the user presses either ALT or F4, but I can’t get it to cancel that key press. Is there a way to make the browser think the user pressed another key as well so the combo would be ALT + G + F4 for example, which would disrupt the ALT+F4 combo?

//Run on keydown, disable user from quiting via ALT+F4
document.onkeydown = function(evt) {
    evt = evt || window.event;
    //Get key unicode
    var unicode = evt.keyCode ? evt.keyCode : evt.charCode;

    //Check it it's ALT or F4 (115)
    if (unicode == 115 || evt.altKey == 1)
    {
        window.event.cancelBubble = true;
        window.event.returnValue = false;
    }
};

Answer

That key event is (on most OSs I guess) processed by the OS before it’S even sent to the browser, so cancelling the event inside the browser won’t help a thing – even if it was Javascript that is executed inside the browser’s UI, not only the current document.

Therefore – what you’re trying to do cannot be done.



Source: stackoverflow