Click here to Skip to main content
15,887,821 members
Articles / Programming Languages / Javascript
Tip/Trick

Disabling browser's back button

Rate me:
Please Sign up or sign in to vote.
1.91/5 (7 votes)
6 Aug 2013CPOL 68.9K   5   10
Disabling the browser's back button.

Introduction  

A web browser has back and forward buttons to navigate between webpages, whose URLs are stored in the browser's history.

Problem Statement

When you are using web forms and authenticated pages you don't want the end user to use back and
forward buttons of the web browser  to navigate as you will be providing all the navigation
required through your application's menu based on workflow.  

The most common problems an application designer should consider are as follows:

  1. Resubmission of web form data, which might create duplicate entries.
  2. Issues with workflow of the application.  
  3. Compromised application security.

Even though if you somehow tweak the web browser to disable the back and forward buttons, you are destroying the fundamental functionality of the web browser i.e. navigation. You might require back and forward buttons when you are browsing static pages. 

But technically disabling the browser's back button is not possible through webpage code, how ever
there is a workaround for it.

Workaround  

The following workaround should be added in the <head> section your  webpage/master page, where you don't want the user to be revisiting using a browser's back button:

HTML
<script language="javascript" type="text/javascript">
    window.history.forward();
</script> 

Compatibility verified in

  1. Internet Explorer 
  2. Google Chrome  
  3. Mozilla Firefox  
  4. Opera 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Program Manager
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThxs for the nice and useful snipet :) Pin
Vendito24WEB7-May-14 0:36
Vendito24WEB7-May-14 0:36 
QuestionDisabling Browser's Back Button Pin
dgtheboss17-Nov-13 8:17
dgtheboss17-Nov-13 8:17 
SuggestionThis is not sufficient and generic way to handle browser back button Pin
Sanjay Gupta (Tech Archit)12-Sep-13 13:58
Sanjay Gupta (Tech Archit)12-Sep-13 13:58 
SuggestionRe: This is not sufficient and generic way to handle browser back button Pin
VICK8-Jan-14 19:00
professional VICK8-Jan-14 19:00 
GeneralMy vote of 1 Pin
HaBiX7-Aug-13 19:36
HaBiX7-Aug-13 19:36 
QuestionMy dev side says yes, but my user side says no! Pin
ryanoc3337-Aug-13 2:54
ryanoc3337-Aug-13 2:54 
GeneralMy vote of 2 Pin
AlexCode6-Aug-13 20:38
professionalAlexCode6-Aug-13 20:38 
GeneralRe: My vote of 2 Pin
Srinivas Kalabarigi6-Aug-13 20:52
professionalSrinivas Kalabarigi6-Aug-13 20:52 
GeneralRe: My vote of 2 Pin
AlexCode6-Aug-13 21:04
professionalAlexCode6-Aug-13 21:04 
I understand that you're pointing at authenticated sites, but the only real thing you should be preventing with this is to someone browsing back after logout and see protected data.
No modification or refresh should be possible and the backend should require authentication.

Another issue is that you're relying on javascript to ensure this kind of security.
javascript can be disabled at browser level and although your application might not work properly without javascript, in this particular case you're addressing it actually doesn't matter as you're accessing a static cached page.

So I keep my opinion, this is not a good idea...
GeneralMy vote of 2 Pin
damian-piatkowski6-Aug-13 10:56
damian-piatkowski6-Aug-13 10:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.