[thelist] Disabling Back Button with Alert

Kowalkowski, Lee (ASPIRE) lee.kowalkowski at hmrcaspire.com
Fri Nov 4 03:22:09 CST 2005


> -----Original Message-----
> From: thelist-bounces at lists.evolt.org
> [mailto:thelist-bounces at lists.evolt.org]On Behalf Of Bob Haroche
> Sent: Thursday, November 03, 2005 5:06 PM
> To: Evolt
> Subject: [thelist] Disabling Back Button with Alert
> 
> I need to disable the back button within a portion of an intranet web
> application (to avoid losing session variables). I know the
> js to do this but I'd also like to pop up an alert to let the 
> user know
> why the back button is disabled. The following doesn't work:
> 
> <script>
> function goforward() {
>       if(window.history.forward(1) != null)
>                        alert('We disabled the back button, 
> blah, blah');
>                        history.forward();
> }
> </script>
> 
> <body onLoad="goforward()">
> 
> It seems that once the browser receives the history.forward() 
> instruction, it ignores all else.

You could say that, or you could say it unloads the current document, so
processing ceases.  In which case you can use onbeforeunload to display your
alert:

    function displayInfo()
    {
      alert('We disabled the back button, blah, blah');
    }

    function goforward()
    {
      var oldOnbeforeunload = window.onbeforeunload;
      window.onbeforeunload = displayInfo;
      history.forward(1);
      window.onbeforeunload = oldOnbeforeunload;
    }

The drawback of this is that if your user skips back x pages, they'll get x
alerts.

This isn't really disabling your back button.  The only way I know of doing
that is to use location.replace() for every interaction, can be really messy
for forms though.  This can also be misleading if the portion of your
intranet application isn't started in a new browser window, as the back
button would not be greyed out and go immediately to the page prior to this
unbackable portion.

-LK


===========================================================
Our e-mail domain has now changed from iraspire.com to hmrcaspire.com. Please update your address books.
===========================================================




More information about the thelist mailing list