Javascript 捕获刷新并将页面更改为index.html
我有个问题。我想检测用户是否按下了F5或刷新按钮。如果是这样,我想将页面更改为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 }); }); 但它不起作用。有人有解决办法吗?我怎
$(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您的控制台中是否有任何错误?