当用户按下刷新按钮或后退按钮时重定向

发布于 2024-10-22 19:08:26 字数 801 浏览 4 评论 0原文

一旦用户按下刷新或浏览器后退按钮,我想将用户重定向到其中一个页面以重新启动应用程序。我的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>

但我不知道为什么它不起作用。我不想使用确认框。

后续: 我已经更新了一些代码。它将用户带到选择页面并要求保留在选择页面或继续工作。但当用户单击“确定”继续工作时,上次提交的数据将再次提交。

  function confirmBrowseAway()
  {
     if (!workIsDone) {
      window.location.href="/choice_page/";
      return "Since you pressed refresh or back button, you are on start page again.";
     }
  }

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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我的影子我的梦 2024-10-29 19:08:26

你可以尝试这个,但它只适用于 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>

You could try this, but it only works in IE (ie8 that I tested):

<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>
心意如水 2024-10-29 19:08:26

这里是如何检测刷新

Here is how to detect the refresh

傲影 2024-10-29 19:08:26

在解决方案中进行一些更改window.onbeforeunload可能会多次触发为我工作。
谢谢大家,
苏尼尔·乔汉

Doing some here and there changes in the solution window.onbeforeunload may fire multiple times worked for me.
Thank you all,
Sunil Chauhan

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文