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

Answers


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