Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 捕获刷新并将页面更改为index.html_Javascript_Jquery_Jquery Mobile - Fatal编程技术网

Javascript 捕获刷新并将页面更改为index.html

Javascript 捕获刷新并将页面更改为index.html,javascript,jquery,jquery-mobile,Javascript,Jquery,Jquery Mobile,我有个问题。我想检测用户是否按下了F5或刷新按钮。如果是这样,我想将页面更改为index.html。这是我的密码: $(window).on('beforeunload',function(){ $.mobile.changePage($(document.location.href="/index.html"),{ transition:"slide", changeHash:false }); }); 但它不起作用。有人有解决办法吗?我怎

我有个问题。我想检测用户是否按下了F5或刷新按钮。如果是这样,我想将页面更改为index.html。这是我的密码:

$(window).on('beforeunload',function(){

    $.mobile.changePage($(document.location.href="/index.html"),{
        transition:"slide",
        changeHash:false
    });

});
但它不起作用。有人有解决办法吗?我怎样才能做到这一点


谢谢。

只需获取键值:

$(document).keypress(function(e) {
if(e.which == 116) {
    window.location.href = '/index.html'; 
}
});
替代方法:

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) {
         window.location.href = '/index.html'; 
        wasPressed = true;
    }else {
        alert("Window closed");
    }
 }

@请尝试我的替代方法,结果与以前相同。我的位置仍然是localhost:8080/秒。html@Bleistift你能告诉我你index.html的路径是什么吗?@Bleistift是localhost:8080/index的路径。html@Bleistift您的控制台中是否有任何错误?