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 bo would be ALT + G + F4 for example, which would disrupt the ALT+F4 bo?
//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;
}
};
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 bo would be ALT + G + F4 for example, which would disrupt the ALT+F4 bo?
//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;
}
};
Share
Improve this question
asked Nov 12, 2013 at 19:51
Martyn BallMartyn Ball
4,8859 gold badges63 silver badges144 bronze badges
5
- 5 Not possible. JavaScript, which is sandboxed within the browser, can't prevent a user from interacting with his window manager or operating system. (And I'm very thankful for that.) (Also, Alt-F4 isn't the only way to close something and only applies to Windows environments anyway.) – David Commented Nov 12, 2013 at 19:54
- @David -- Not yet... evil thoughts – tymeJV Commented Nov 12, 2013 at 19:54
-
You're better off doing your sign-out script JavaScript on the
window.unload()
event, which triggers when the user closes the window & not preventing the default mechanism - Or, keep Session in Cookies to match when the person re-logs in, if no match. Log out. – MackieeE Commented Nov 12, 2013 at 19:54 -
1
Do you need to do that only for
Alt+F4
or for any of the 1001 ways to close a window? – Vatev Commented Nov 12, 2013 at 19:55 - 1 Are you also going to detect when the power goes out, the network connection goes down, Or the puter crashes? If not, you should think of a better way to detect if the user disconnects. – some Commented Nov 12, 2013 at 19:58
3 Answers
Reset to default 10That 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.
One can prevent the resulting window closure with:
window.onbeforeunload = funcRef
I think readers landing here might want to be reminded of this (as I did).
For related details, see Jquery prevent window closing
try to use the code below:
window.webContents.on("before-input-event",(event,input)=>{
if(input.code=='F4'&&input.alt)event.preventDefault();});