Search This Blog

Tuesday, June 3, 2008

How to Disable the Back button...

In my queries against the Internet in hopes of finding a clean way of keeping users from using the back button in my application I have found a simple way to go about this. Let me first start off with the problem at hand. Many of us writing code for the web experience this all the time with users using the backward and forward buttons of their browsers when testing and working with our applications. So, say for example we have some pages that we don't want the user to be able to hit the back button and go to because if they do our code will possibly break, blow up, or insert another record. All of these and many others are not good.

So lets look at some of the obvious examples to fix this problem. One possibility is to close the current window and open another window that does not have the toolbar enabled. That may work for some, but is not the best approach. Another approach out there is to clear the browser's cache and force the client to request the web page again. This works just fine but you will also need to make use of session variables to keep track of whether or not the user has already visited this page in the current session. Another method that I find pretty easy and straight-forward to use is with some simple javascript. Here is an example: (Blogger doesn't like html or body tags in their posts, so be creative.)

"body onload="history.go(1)

From this example any web page that you include this javascript in will not allow the browser to ever return to the previous page. Basically, what is happening is that the javascript is telling the browser when the page loads to go forward 1 page in the history collection. Pretty neat, huh? This works well for scenarios where you want the user to navigate around your site using links and navigation elements. This method is actually better in some cases in that it will use the cached page instead of going back to the server. One thing to note is that there is no real way of disabling the browser's buttons. If anyone has any other cool ways of doing this please respond to this post.

1 comment:

John said...

We would like to nominate you for a Best of Blog Entry.

Please visit our site at: to submit your blog entry!

Benefits include:
1) Permanent Backlinks to your blog
2) Fully SEO optimized for maximum exposure
3) A chance to be published into a top 500 best of blog book"