抓取刷新并将页面更改为index.html

时间:2015-03-06 09:54:15

标签: javascript jquery jquery-mobile

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

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

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

});

但它没有用。有谁解决方案,我怎么能做到这一点?

感谢。

1 个答案:

答案 0 :(得分:0)

获取关键值:

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

Key Reference

替代方法:

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");
    }
 }