Sunday 4 November 2012

Clear Browser History using Javascript


Clear Browser History using Javascript


If you need to clear the Browser History, after arriving on a particular page (e.g. arriving to Login page after Logout), below is the wicked way.
This script needs to put in only on one page, from where you do not want to move back.

<script type="text/javascript">
        window.onload = function () { Clear(); }
        function Clear() {          
            var Backlen=history.length;
            if (Backlen > 0) history.go(-Backlen);
        }
</script>


Second, if you do not want the user to move to the back page, you can do as below. This script needs to be repeated at every page.


<script type="text/javascript">
       if(window.history.forward(1) != null)
           window.history.forward(1);
</script>


5 comments:

  1. Dear Saurab, You could try the following trick if you want to disable the browser back button:

    <script type = "text/javascript" >
    function disableBackButton()
    {
    window.history.forward();
    }
    setTimeout("disableBackButton()",0);
    </script>

    <body onunload="disableBackButton()" onload="disableBackButton()">
    </body>


    Alternatively, Refer these links:

    http://www.dotnetvishal.com/2012/11/disable-browser-back-button.html

    http://forums.asp.net/p/1161997/1923379.aspx

    ReplyDelete
  2. It works perfectly, thanks Ranjan!

    ReplyDelete
  3. Perfect Code to disable back button.....Thanks

    ReplyDelete

Thank You for Your Comments. We will get back to you soon.

back to top