To read the article online, visit http://www.4GuysFromRolla.com/webtech/111500-1.2.shtml

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!

    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">
    <!--
      javascript:window.history.forward(1);
    //-->
    </script>
    

    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


  • Article Information
    Article Title: A Thorough Examination of 'Disabling the Back Button.', Part 2
    Article Author: Akhilesh Reddy
    Published Date: Wednesday, November 15, 2000
    Article URL: http://www.4GuysFromRolla.com/webtech/111500-1.2.shtml


    Copyright 2014 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers