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:
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开发者_如何学Python 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):
<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>
Here is how to detect the refresh
Doing some here and there changes in the solution window.onbeforeunload may fire multiple times worked for me. Thank you all, Sunil Chauhan
精彩评论