Link to home
Start Free TrialLog in
Avatar of skundu
skundu

asked on

Disable browser "Back" button

how can I disable the browser's "Back" button in a HTML page?
I do not want to create JavaScript window to avoid showing any toolbar,etc.
In regular HTML page, is there any way to disable the back button in the toolbar?

thanks.
-skundu
Avatar of apprenti
apprenti

If you think this is a good functionality to implement, you've got some very strange ideas.
No, you will need to use a child window for this. and disable toolbars,etc in it.

Avatar of skundu

ASKER

Some commercial web pages does not let you go back to previous page (leaving their web site). How do they do that? By some way but not the javascript?

ASKER CERTIFIED SOLUTION
Avatar of Saqib Khan
Saqib Khan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
<a href="somwhere.asp" onclick="location.replace(this.href);return false;">TEST</a>

location.replace will not let you go back to the original page.
Avatar of CRAK
onLoad="if(history.length>0)history.go(+1)"
You said ...
"Some commercial web pages does not let you go back to previous page (leaving their web site). How do they do that? By some way but not the javascript?"

A way to do that without javascript is to use a meta refresh tag in the entry page to your site.  If you have a banner ad or a link to you from another site you can prevent the user from going back (leaving your site). Put the following meta tag in the <head></head> section of your page to redirect to page2.html after 0 seconds.

<meta http-equiv=refresh content="0, URL=page2.html">

You could put this in your home page and make page2.html your home page content.



onLoad="if(history.length>0)history.go(1-history.length)"
guenze u ok buddy:). Question is been answered already.
wow that's a terrible user experience!!!
how about using a document.location.replace() instead? then the page will be removed from the history otherwise the user will click back and end up on the page they started on (not very nice).  
simply use this code in head tag u will block user to go back through back button :) so simple it is


<script language="javascript">
      window.history.forward(1);
</script>
This code is useful in following conditions.

1)  When you dont want the round trip to server
2)  When the page contents contains dynamically created  objects. ( u will loose all the stuff u entered after refresh)

===============================
code follows

 var gotBodyFocus=0; // used in gotBFocus() function.

function escBackSpace() // called on document.body keydown event
 {
    if(event.keyCode == 8)
       {
          if  (gotBodyFocus == 1 | document.activeElement.getAttribute("type") != "text") // to avoid the backspace event except the Textbox
           {
             //event.keyCode = 0;
             event.returnValue=false
           }
           
       }    
 }

function gotBfocus() //called on document.body onfocus event
{
  gotBodyFocus = 1;
 }

function lostBfocus() // called on document.body onblur event
{
  gotBodyFocus = 0;
}


<body MS_POSITIONING="GridLayout" onkeydown="escBackSpace();" onblur="lostBfocus();" onfocus="gotBfocus();">
        <form id="Form1" method="post" runat="server" onkeydown="escBackSpace();" onblur="lostBfocus();">
above Solution by vac is Strictly for IE, I would not use it in my application
You may try the following options from JavaScript,

1. If you move from page1 -> page2, and want to disable the browser back in page 2,then add the following at the top of page1.

<script>
if(window.history.forward(1) != null)
                 window.history.forward(1);
</script>
This works for both IE and NS

2.You may also replace the page in the history with the current page itself so that there is no page to go back.
                  location.replace(this) would do this.

3.If you are using JSP and not HTML, the best thing would be to prevent the JSP page from caching itself.


Theres another way you can do it.
Simply hide the back button and use a javascript to cancel out the Backspace button on the keyboard.

<script>
function KeyCache() {
  if (window.event && window.event.keyCode == 8) {
    window.event.keyCode = 123; //Replaces with F12 button. (Does nothing in IE)
  }
}
document.onkeydown=KeyCache();
</script>
An alternative is

<script>
history.forward();
</script>
 
Put this anywhere on the page you dont want to go back to.
We need a better solution.  Here's a new scenario.  I have a form that that I send to the user and I put different parts of the form in <Div></Div> pairs that I wrap around different parts of the form.  I set the div attributes to hidden and only make visible the portion of the form that I want the user to fill in.  Based on that input, I will hide the portion of the form they just completed and either show a new portion of the form or submit the form for processing.  I need to keep the user from going back within that page so that I can properly manage the visiblity of the different parts of the form until the form is sent to the server for final process, after which I can return control of the browser back to them.  Any creative solutions to this one?
well there seem to be a litle confusion about cache and history, i can disable cache and still be able to navigate back using several processes, even if cache of that page is disabled that means only that the page will be refreshed from the server, it solves most of the cases. But sometimes we dont want the history back thing.. well it seems wiser to me to prevent the url from entering the history list (this can be done sometimes using a location.replace) than catching all atempts to navigate back in history list. Of course this wont be good for everyone, in my case i dont mind user goes back to some other site but dont want to allow the resubmition/refresh of my page because it is not stateless the content displayed is always in conformity with the server stored state, meaning that going back and requesting some other thing from that page will bring confusion because the server responds only to the stored state (normal site and app's work on a stateless schema, all info needed to make an operation is submitted. not my case)
I did this unintentionally before on a shopping cart page for a commercial website I developed and we actually lost (at least one) customer because of it...

-Scotty
Add this to the bottom of your page:

<script>
window.onbeforeunload = function () {
   // Disable back.
}
</script>

This works on IE, Firefox and Safari. Its used to give warnings when people leave your page, by any means, except for forms and links. Like closing the window or pressing then back button.
As a side effect the browser stops caching previous pages, if you press back you just return to the same page. We have been using this technique in a browser game, so that you dont get session and sql errors, cause people change pages and cookie and then return to a older view. ( see http://booty.game-host.org/ for an example.)

For more info:
http://www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript