aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Disabling the Back button of the browser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Disabling the Back button of the browser" Watch "Disabling the Back button of the browser" New topic
Author

Disabling the Back button of the browser

Naik Priyanks
Greenhorn

Joined: Nov 15, 2004
Posts: 10
Hi,
How can I disable the back button of the browser?
I want to stay on the same page when the user clicks the back button without doing any action.
Any help would be appreciated.
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
HTML/Javascript is more apt for this thread, I guess.

Simple answer: You can't.

There are ways to disable the back button (by removing the tool bar etc), but there are lot of other ways using which the user can navigate to the previous page (using Backspace button, right click and click 'Back' etc)

The proper way to solve the problem is to design your system in a way to manage such behaviours (back, forward, refresh etc).


Mani
Quaerendo Invenietis
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

How can I disable the back button of the browser?


You cannot disable back browser button from server side code


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Ajith George
Ranch Hand

Joined: Dec 22, 2005
Posts: 109
You can disable back button of your browser by including the following javascript on that page.

or

This will work only on IE. i think so. But this is not the correct way of doing as client may use other browsers also.


SCJP 1.4, Brainbench
LinkedIn - Blog
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Ajith George:
You can disable back button of your browser by including the following javascript on that page.

I tried this in IE and it causes the page to automatically send you back one page, which is close to the opposite of "disabling the back button"! Are you sure this is what you meant?


There is no emoticon for what I am feeling!
Ajith George
Ranch Hand

Joined: Dec 22, 2005
Posts: 109
Sorry there was a mistake. The correct way of doing is


Try this !
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Ajith George:
Sorry there was a mistake. The correct way of doing is



That doesn't seem to work either. Try it:

Are you sure that you know how to do this?
Ajith George
Ranch Hand

Joined: Dec 22, 2005
Posts: 109
It's working for me. This is code for test2.jsp
And this is code for test3.jsp

Both jsp's have almost same code and both have links between them.
When i reached test3.jsp from test2.jsp, ican't return back using my back button. Vice versa too it's working. Try this !
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Thanks! I understand now Here's a simpler example, because your example with links going between two pages hides the hack:

When you visit page 1 the back key is functional (this is why I thought history.forward isn't working). Then you follow the link to page two, and the back button is still *enabled* but when you click on it *that's* when the forward method successfully fires and sends you back to page 2. Maybe I'm hoping for too much, but is there a solution that truly *disables* the back button?
[ March 29, 2006: Message edited by: Jeff Albertson ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

Moved to HTML.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You can not disable the back button, you can not remove it, you can not defeat my button on my mouse or combinations on the page.

What if JavaScript is disabled, and all of that other stuff.

As I always tell people: Build your application around the browser!

Eric
Naik Priyanks
Greenhorn

Joined: Nov 15, 2004
Posts: 10
Thanks all for ur valueable input.
Vikram Waikar
Greenhorn

Joined: Jul 26, 2002
Posts: 5
with all due respect to good design decisions and best practices , isn't there a simple hack for this ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

No doubt there is a simple hack. But there is not a solution to the problem that "disabling the back button" is supposed to solve.

Let's suppose you manage to disable the back button on my browser. When I find you have done that I mutter something about your mother. Then I right-click on the link, and open the new page in a separate tab. Then when I want to go back to the page I just came from, it's right there in the first tab. Not only is your problem not solved but you provided me with an annoying interface as well.

At this point the true fanatic will try to prevent me from right-clicking on links. And to prevent me from using that drop-down thing next to the back button which lets me go back to any of the last few pages I viewed. And to prevent me from using the "Back" option on my browser's menu. The professional will get a life and write their server to work properly when these browser features are used.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

And doing silly things like disabling browser buttons and the right-click menu will bring you fame:

http://www.webpagesthatsuck.com/worst-web-design-featured-on-web-pages-that-suck-in-2005.html

But probably not the type you are wanting.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Disabling the Back button of the browser