禁用ALT + F4,是的,我知道不建议这样做 [英] Disable ALT+F4, yes I know it isn't recommended
问题描述
我需要一个JavaScript脚本,禁止用户使用ALT + F4关闭。我到处都看,但只是每个人都说不建议。
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.
这是我当前的脚本,它会检测用户是按ALT还是F4,但我无法取消该按键。有没有办法让浏览器认为用户按下了另一个键,所以组合将是ALT + G + F4,这会破坏ALT + F4组合?
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;
}
};
推荐答案
那个关键事件是(在我猜的大多数操作系统上)在操作系统甚至被发送到浏览器之前进行处理,因此取消浏览器中的事件将无济于事 - 即使它是在浏览器的UI内执行的Javascript,而不仅仅是当前文档。
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.
这篇关于禁用ALT + F4,是的,我知道不建议这样做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!