[英]Redirecting when user presses refresh button or back button
Once user presses refresh or browser back button,then I want to redirect the user to one of the page for restart the application.My javaScript code is as follow: 一旦用户按下刷新或浏览器后退按钮,那么我想将用户重定向到页面之一以重新启动应用程序。我的javaScript代码如下:
var workIsDone = false;
window.onbeforeunload =confirmBrowseAway;
function confirmBrowseAway()
{
if (!workIsDone) {
alert("You are moving to choice page");
window.location.href="/choice_page/";
}
}
function abc(){
workIsDone=true;
}
//.
//.
<div align="center"><input class="button blue" type="Submit" value="Next" onClick="abc()"></div>
But I don't know why it is not working. 但是我不知道为什么它不起作用。 I don't want to use confirm box.
我不想使用确认框。
Followup: I have updated the code a bit. 后续行动:我对代码进行了一些更新。 Its taking the user to choice page and asking to remain on the choice page or carry on with the work.
它带用户到选择页面并要求保留在选择页面上或进行工作。 But when user click OK to carry on the work, the last submitted data is again submitted.
但是当用户单击“确定”进行工作时,将再次提交最后提交的数据。
function confirmBrowseAway()
{
if (!workIsDone) {
window.location.href="/choice_page/";
return "Since you pressed refresh or back button, you are on start page again.";
}
}
You could try this, but it only works in IE (ie8 that I tested): 您可以尝试一下,但是它仅在IE(即我测试过的IE8)中有效:
<script language="javascript">
function document.onkeydown() {
if (event.keyCode == 116) {
alert("MOVING AWAY");
window.location.href="/choice_page/";
event.keyCode = 0;
event.cancelBubble = true;
return false; }
}
</script>
这是检测刷新的方法
Doing some here and there changes in the solution window.onbeforeunload may fire multiple times worked for me. 在解决方案窗口中进行一些更改。onbeforeunload可能会多次触发我的工作。 Thank you all, Sunil Chauhan
谢谢大家,Sunil Chauhan
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.