2

I wrote the following code to clear the browser history and it's working correctly in Internet Explorer but it does not work in Mozilla Firefox. How can I solve this problem?

<script language="JavaScript">
function DisablingBackFunctionality()
{
    var URL;
    var i ;
    var QryStrValue;
    URL=window.location.href ;
    i=URL.indexOf("?");
    QryStrValue=URL.substring(i+1);
    if (QryStrValue!='X')
    {
        window.location.href="http://localhost:8085/FruitShop/";
    }
}
</script>

I'm writing this code in <header> section.

Jakub Hampl
  • 39,863
  • 10
  • 77
  • 106
John
  • 21
  • 1
  • 1
  • 2

1 Answers1

7
  1. Do NOT try to break the back button
  2. Instead on the page you want not to return to use location.replace(url)

Also your code can be vastly simplified - but be aware it does not CLEAR the history. You cannot clear the history, only keep a page from getting into the history or as you try, break the back button

function DisablingBackFunctionality() {
// get the query string including ?
  var passed =window.location.search; 
// did we receive ?X
  if (passed && passed.substring(1) =="X") { 
// if so, replace the page in the browser (overwriting this page in the history)
    window.location.replace("http://localhost:8085/FruitShop/"); 
  }
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • @John please tell how and where you call the function – mplungjan Mar 13 '11 at 16:04
  • @mplungjan Can you explain how to use this function? I need IE requirement only. – LCJ Nov 08 '12 at 07:49
  • I have a custom logout page. When user clicks logout this page is reached (and status is saved in database). Do I need to use the above listed function in each of my pages? On which event I can call this? – LCJ Nov 08 '12 at 08:59
  • I suggest you ask this as a new question since it is not exactly the same scenario. You would call the script from the logout page only – mplungjan Nov 08 '12 at 09:00
  • Just add it to the logout page in the head – mplungjan Nov 08 '12 at 09:46
  • Can you elaborate your answer in http://stackoverflow.com/questions/13286445/better-way-to-handle-logout ? – LCJ Nov 08 '12 at 09:47