Saturday, July 18, 2009

disable Back button of a browser?

Most of time we want to disable the " Browser Back" button due to some security reason or any

other purpose.

there are some tech. to do this .

  • You can use the location replace method when changing the location.




  • <script language="JavaScript">
    <!--
    if (document.images)
    location.replace('http://www.xyz.com/login.html');
    else
    location.href = 'login.html';
    //-->
    </script>


    In this case "replaces the current history location with the new location."


  • You can load the new location into another window, and then close the old window:


  • <script langauge="JavaScript">
    <!--
    function newWindow(Nameoffile,windowName)
    {
    msgWindow=window.open(fileName,windowName);
    }
    //-->
    </script>
    <a href="javascript:newWindow('welcome.html','window1')">Open Window</a>


  • open in the next window:



  • <a href="login.aspx" target="_blank">New User Login </a>

    So it will open login page in new window

  • You can open a window without a toolbar and Disable backButton from Keyboard and

    Rightclick



  • <script language="JavaScript">
    <!--
    msgWindow=window.open('Login.aspx','windowName','toolbar=no');
    //-->
    </script>


  • You can add code to the previous page to force the browser to go forwards again:



  • <script language="JavaScript">
    <!--
    javascript:window.history.forward(1);
    //-->
    </script>





There is no direct way to catch the back button event, so you have to play with javascipt to do so.

happy coding ........

3 comments:

  1. no code is running properly in mozilla but they r working in IE

    ReplyDelete
  2. which error u r getting exactly on error console

    ReplyDelete