当用户按下刷新按钮或后退按钮时重定向
一旦用户按下刷新或浏览器后退按钮,我想将用户重定向到其中一个页面以重新启动应用程序。我的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以尝试这个,但它只适用于 IE(我测试过的 ie8):
You could try this, but it only works in IE (ie8 that I tested):
这里是如何检测刷新
Here is how to detect the refresh
在解决方案中进行一些更改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