admin管理员组

文章数量:1420606

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="" >
<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

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/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

Share Improve this question asked May 2, 2012 at 12:14 user1345801user1345801 592 silver badges5 bronze badges 2
  • i'm also interested in this issue! what do you do if the client closes the tab? – p0rter Commented May 2, 2012 at 12:16
  • @p0rter In that case we cont do anything but for this case we can try to give the solution. – user1345801 Commented May 2, 2012 at 12:20
Add a ment  | 

4 Answers 4

Reset to default 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.

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

See Disabling Back button on the browser

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.

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.

本文标签: javascriptHow to control the browser buttons using java scriptStack Overflow