How to control the browser buttons using java script?

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>
<SCRIPT type="text/javascript">
function noBack() { window.history.forward(); }
<BODY onload="noBack();"
onpageshow="if (event.persisted) noBack();" onunload="">
<a href='page2.html' >Page 2</a>

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


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
  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 recommended 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.

Need Your Help

“Transcode” copying video/audio codec

linux ffmpeg h.264 transcode image-stabilization

I took some very shaky video, and went online to try and find how to stabilize it. I came upon this nice blog that uses transcode for it.

use regular expression in if-condition in bash

regex bash

I wonder the general rule to use regular expression in if clause in bash?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.