Javascript 在window.onbeforeunload事件中使用window.event.keyCode在javascript中捕获f5按键事件总是0而不是116

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14707602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:41:37  来源:igfitidea点击:

capturing f5 keypress event in javascript using window.event.keyCode in window.onbeforeunload event is always 0 and not 116

javascriptjquery

提问by ismail baig

i am creating an MVC application.There was a neccessitity to make a variable in a session to null upon closing of the application (i.e. window/tab) but not upon refreshing the application. I tried it through the following code.

我正在创建一个 MVC 应用程序。关闭应用程序(即窗口/选项卡)而不是刷新应用程序时,必须将会话中的变量设为 null。我通过以下代码进行了尝试。

<script type="text/javascript">
           window.onbeforeunload = function (e) {
               e = e || window.event;
               if (window.event.keyCode == 116) {
                   alert("f5 pressed");
               }
               else {
                   alert("Window closed");
                   //call my c# code to make my variable null, eg:Session["myVariable"] = null;
               }  
           };
</script>

But when F5 is pressed then, "window.event.keyCode" is always 0 and not 116. Because of which my variable is becoming null even upon F5 key press which is not my requirement.

但是当按下 F5 时,“window.event.keyCode”始终为 0 而不是 116。因此,即使按下 F5 键,我的变量也会变为空,这不是我的要求。

Even when the application (i.e. webpage) is closed,even then its 0 (which is probably correct).

即使应用程序(即网页)关闭,即使它是 0(这可能是正确的)。

Please note that the above part of the code is in .cshtml file.

请注意,上述部分代码在 .cshtml 文件中。

Can anyone tell where am i wrong ?

谁能告诉我哪里错了?

回答by meo

You have to listen to different events if you want this to work crossborwser + you have to listen to the key-event every time its pressed, not on load:

如果你想让它在crossborwser上工作,你必须听不同的事件+你必须在每次按下时听键事件,而不是在加载时:

document.onkeydown = fkey;
document.onkeypress = fkey
document.onkeyup = fkey;

var wasPressed = false;

function fkey(e){
        e = e || window.event;
       if( wasPressed ) return; 

        if (e.keyCode == 116) {
             alert("f5 pressed");
            wasPressed = true;
        }else {
            alert("Window closed");
        }
 }

here is a demo: http://jsfiddle.net/FSrgV/1/embedded/result/

这是一个演示:http: //jsfiddle.net/FSrgV/1/embedded/result/

but if you simply want to know if the user quits the page you could simply use window.onbeforeunload: https://developer.mozilla.org/en-US/docs/DOM/window.onbeforeunload

但如果您只是想知道用户是否退出页面,您可以简单地使用window.onbeforeunloadhttps: //developer.mozilla.org/en-US/docs/DOM/window.onbeforeunload

回答by Sim_on

Dont use e.keyCode == 166 use e.code == 'F5' instead.

不要使用 e.keyCode == 166 使用 e.code == 'F5' 代替。

 function fkey(e){
    e = e || window.event;
   if( wasPressed ) return; 

    function fkey(e){
        e = e || window.event;
        if (e.code === 'F5') {
            alert("f5 pressed");
            wasPressed = true;
        }else {
            alert("Window closed");
        }
    }

This is because the 't' and 'F5' both use the keycode number 116. If you go on keycode alone then if the user presses the 't' key your page will refresh.

这是因为 't' 和 'F5' 都使用键码编号 116。如果您单独使用键码,那么如果用户按下 't' 键,您的页面将刷新。

回答by Paul

You could just write it like this:

你可以这样写:

$(document.body).on("keydown", this, function (event) {
    if (event.keyCode == 116) {
        alert('F5 pressed!');
    }
});

回答by Faisal Shahzad

Modified version and working also after pressing 't'.

修改后的版本并在按 't' 后也可以工作。

key 116 getting pressed auto after 84

按键 116 在 84 之后自动按下

document.onkeydown = fkey;
document.onkeypress = fkey
document.onkeyup = fkey;

var wasPressed = false;

function fkey(e){
    e = e || window.event;
   if( wasPressed ) return; 

    if (e.keyCode == 116) {
         alert("f5 pressed");

    }else {
        alert("Window closed");
    }
    wasPressed = true;
}

回答by Shaz

document.onkeydown = disableF5;
document.onkeypress = disableF5
document.onkeyup = disableF5;    
function disableF5(e) { if ((e.which || e.keyCode) == 116) e.preventDefault(); };

回答by reza akhlaghi

dont use (e.keyCode == 116) to ckeck F5 ; instead of use this

不要使用 (e.keyCode == 116) 来检查 F5 ;而不是使用这个

<script>
    document.onkeydown = capturekey;
    document.onkeypress = capturekey;
    document.onkeyup = capturekey;

    function capturekey(e) {
        e = e || window.event;
        //debugger
        if (e.code == 'F5') {
            if (confirm('do u wanna to refresh??')) {
                //allow to refresh
            } 
            else {
                //avoid from refresh
                e.preventDefault()
                e.stopPropagation()
            }
        }
    }
</script>

回答by senior

I agree with meo's solution, but I will add some modifications.

我同意 meo 的解决方案,但我会添加一些修改。

when we use document.onkeydown = fkey; for example and in the page we have another method affected to the document.onkeydown then the browser will detect only the last event. However when we use : jQuery(document).on("keydown",fkey); even if we have another function to handle the keydown event, all the functions will be triggered.

当我们使用 document.onkeydown = fkey; 例如,在页面中,我们有另一个影响 document.onkeydown 的方法,那么浏览器将只检测最后一个事件。然而,当我们使用: jQuery(document).on("keydown",fkey); 即使我们有另一个函数来处理 keydown 事件,所有的函数都会被触发。

see the next example to more understand:

请参阅下一个示例以进一步了解:

var wasPressed = false;
document.onkeydown = f1;
document.onkeydown = f2;

jQuery(document).on("keydown",f3); 
jQuery(document).on("keydown",f4); 
function f1(e){
e = e || window.event;
if( wasPressed ) return; 

        if (e.keyCode == 116) {
             alert("f5 pressed");
            wasPressed = true;
        }else {
            alert("Window closed");
        }
}
function f2(){
    alert('f2');
}
function f3(){
    alert('f3');
}
function f4(){
    alert('f4');
}

what will be shown here: only 3 alerts: f2,f3 and f4. and the f1 function isn't triggred in this example.

这里将显示什么:只有 3 个警报:f2、f3 和 f4。并且 f1 函数在这个例子中没有被触发。