When you think ASP, think...
Recent Articles xml
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips
spgif spgif

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
spgif spgif
ASP ASP.NET ASP FAQs Feedback topnav-right
Print this page.
Published: Wednesday, November 15, 2000

A Thorough Examination of "Disabling the Back Button.", Part 2

By Akhilesh Reddy

  • Read Part 1

  • In Part 1 we examined a few potential ways to disable the back button, a commonly asked question on the ASPMessageboard. In this part we'll continue our examination of various techniques for preventing the user to return to the previous page!

    - continued -

    My next area of research focused on the various rewiring the back button suggestions. An article by TJ Sylvester, Rewiring the Back Button, makes interesting reading but I noticed that when one clicks back it does not indeed take you to the page you entered the data but if I clicked back twice it does and we would not want that too. Basically a determined user could always figure out a way to circumvent the preventative measures.

    Another way to "disable the back button" is to use client-side JavaScript code to open a new window that doesn't have the toolbar. (This makes it harder (not impossible) for the user to go back to the previous page.) Another, more failsafe approach (although quite annoying) is, when a form is submitted, to open a new window and close the window that the form existed in. I didn't give this method serious thought because I would not like my site opening up a new window everytime a user submitted a form.

    Next I examined the possibility of adding client-side JavaScript code on the page that we do not want to let the user return to. Such JavaScript code could be used to have the effect of hitting the forward button, which would counter any action by a user clicking the back button. The JavaScript code to accomplish this can be seen below:

    <script language="JavaScript">

    Again this is workable but it is far from the best way. I was then given the suggestion to use location.replace to navigate form one page to another. What this does is it replaces the current history entry with the new page so only one page will be maintained in the history and the back button will never get enabled. This is, I guess, what a lot of people are looking for, but again this would not be the best answer in all cases.

    For one thing you will have to use client side script for this. For an anchor tag this will be easy by just using:

    <A HREF="PageName.htm" onclick="javascript:location.replace(this.href); event.returnValue=false; ">No back button when you do this.</A>
    [Try out the above link!]

    The above technique has its disadvantages: simply using Response.Redirect will not work, since, each time a user jumps from one page to another, you need to clear out the location.history field through client-side code. Also, keep in mind that this will just remove the last history entry, not all of them. Go ahead and click the above hyperlink, you will be taken to a simple HTML page. Try clicking the back button and you will notice you will be taken to the page you were visiting before you came to this page! (Assuming, of course, you have client-side JavaScript code enabled in your browser.)

    After my exhaustive search I found that there is still no way of truly disabling the back button for all cases. All the methods I discussed in this article will, with varying degrees of success, prevent the user from viewing the previous page, but they, of course, all have their limitations. The best solution involves a mixture of both client-side and server-side script; regardless, there is no way to completely disable the back button... sorry!

  • By Akhilesh Reddy

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article