-1

I need to disable the browser back button while cliking the submit button in html page to secure the process has not terminated.

Here is the code to disable the back button in browser, but it's working in onload event only. when i am trying to use this in onclick event it wont work.

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>New event</title>
</head>
<SCRIPT type="text/javascript">
window.history.forward();
function noBack() { window.history.forward(); }
</SCRIPT>
</HEAD>
<BODY onload="noBack();"
onpageshow="if (event.persisted) noBack();" onunload="">
<a href='page2.html' >Page 2</a>
</body>
</html>

Can anyone know how to disable the browser back button while i am clicking submit button in html page

user1345801
  • 59
  • 2
  • 5

4 Answers4

3

It won't work because on click, it will run before navigation--so it will go to the nonexistant "forward" page.

Use onbeforeunload

var submitFlag=false;
//set submitFlag to true on onclick of submit button
window.onbeforeunload=function(e){ 
 if(submitFlag){
  return false;
 }
 return "Are you sure you want to leave?"

 }

This cannot force the user though. You can never force the user not to go back. Otherwise, this can lead to big security issues.

Manishearth
  • 14,882
  • 8
  • 59
  • 76
1

Don't do it. It's strongly recommended you do not break the functionality of the back button.

See Disabling Back button on the browser

Community
  • 1
  • 1
psx
  • 4,040
  • 6
  • 30
  • 59
0

You can't disable the browser back button. Not via JavaScript, or anything else. On the server side, you can keep a track of whether your submit button has been pressed and take action accordingly.

karan k
  • 947
  • 2
  • 21
  • 45
0

You cannot disable the browser back button.

The code snippet you give does not "disable" the back button. I am not convinced you understand what it does.

What it does:

  1. In the script tag, simulate a click on the "forward button" (window.history.forward()). That might take us back to where we came from if the user just pressed in the back button.

  2. When the page is loaded (so the previous hack did not work), and the page was loaded from the history cache (event.persisted) then simulate a click on the forward button.

At no point there was the back button disabled. Instead, what you have done is write a page that breaks the back behavior for the following page.

You might want to use the onbeforeunload event instead.

ddaa
  • 52,890
  • 7
  • 50
  • 59
  • Technically you can [disable the browser back button by some hack-ish code involving `pushState` and `onpopstate`](http://stackoverflow.com/questions/10790373/browsers-previous-or-next-button-control/10790394). – Derek 朕會功夫 Nov 15 '12 at 03:27