简体   繁体   English

用户按下刷新按钮或返回按钮时进行重定向

[英]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.

相关问题 用户按下后退按钮时的AngularJS - AngularJS when user presses back button 当用户在 history.push() 之后按下“返回”按钮时,我们可以刷新页面吗 - Can we refresh a page when a user presses "go back" button after history.push() 当用户按下浏览器的后退按钮时关闭Jquery对话框 - Close Jquery dialog when the user presses browser's back button 当用户按下返回按钮时,强制重新提交表单 - Forcing form re-submission when user presses back button 如何在用户按下刷新按钮时限制改组 - how to restrict shuffling while user presses the refresh button 当用户按下F5键或单击浏览器的后退/关闭按钮时,如何显示自定义的花式框? - How to display customized fancybox when user presses F5 key or clicks browser's back / close button? React Router:当用户按下后退按钮时如何重新渲染页面 - React Router: How to re-render the page when the user presses the back button 用户在谷歌浏览器中按“后退”按钮时,如何加载上一个状态? - How can I load last state when user presses back button in google chrome? 用户按下后退按钮后如何启动CoffeeScript / Javascript - How to fire CoffeeScript/Javascript after user presses back button 在动态内容上按后退按钮的困难 - Difficulties with back button presses on dynamic content
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM