How to disable browser's BACK button?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sspost
    New Member
    • Sep 2007
    • 15

    How to disable browser's BACK button?

    how can i disable the back button for a browser?

    I have been reading a lot of forums but cant find any answer to it...other than opening up the page, in which i want to disable the browser in a new window(which i dont want to do)

    Is their a way to do it? using javascript or something?

    I have tried using the following but non of them actually disable the
    back button:

    window.history. forward(1); -- does not disable the back button

    location.replac e(URL) -- keeps flickering my page

    3 approahes from:
    http://www.htmlgoodies .com/tutorials/buttons/article.php/3478911 -- but of no use for me
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    Originally posted by sspost
    how can i disable the back button for a browser?

    I have been reading a lot of forums but cant find any answer to it...other than opening up the page, in which i want to disable the browser in a new window(which i dont want to do)

    Is their a way to do it? using javascript or something?

    I have tried using the following but non of them actually disable the
    back button:

    window.history. forward(1); -- does not disable the back button

    location.replac e(URL) -- keeps flickering my page

    3 approahes from:
    http://www.htmlgoodies.com/tutorials...le.php/3478911 -- but of no use for me
    I don't recall the JavaScript syntax, but if I recall correctly you just have to clear the history object in memory which keeps a list of all the pages visited since you opened the instance of the browser....howe ver you don't have access to the browser's actual history for obvious security reasons. An alternative is to keep a session variable open on the user's session to keep tabs on the page they're supposed to be on so they can't skip back and forth. This will ensure that someone goes through your web pages in the right order (i.e. for an online exam or something).

    JavaScript isn't foolproof because someone could turn off their javascript and bypass your mechanism. My recommendation is using the session mechanism on the server side.

    If you're intent on JavaScript you can always replace the location of the previous history slot with the name of the current location...some thing akin to
    Code:
    window.history.back(0) = window.location
    I forget if the history is in reverse order or not...so you might have to find out the length of the history and replace the value in parenthesis with length-1. This also does't bypass the little drop menu on the back button so here, clicking the back button would cease to take them to anywhere but your page, but they could bypass it by choosing one of the items in the drop menu which would effectively bypass your code.

    Like I said though, that option isn't failsafe...and when they hit back the screen will essentially reload, so yes, you'll get a flicker.

    Comment

    • mzmishra
      Recognized Expert Contributor
      • Aug 2007
      • 390

      #3
      i am not sure if you can disable browers back button progarmatically .

      But there are soem approaches to achieve that functionality
      see the article below
      http://bucarotechelp.c om/design/jseasy/95070401.asp

      Comment

      • sspost
        New Member
        • Sep 2007
        • 15

        #4
        for window.history. back(0) = window.location

        where do i write this code?
        i wrote in <head> but it doesnt work....

        Comment

        • balabaster
          Recognized Expert Contributor
          • Mar 2007
          • 798

          #5
          Originally posted by sspost
          for window.history. back(0) = window.location

          where do i write this code?
          i wrote in <head> but it doesnt work....
          You would have to do fire a JavaScript from the Page OnLoad event

          <Page OnLoad="JavaScr ipt:MyFunction( );">

          Bear in mind that the function call in your OnLoad event is case sensitive just like JavaScript is.

          Something similar could also be done with the meta tag http-refresh...I forget exactly how that works but if you google "pragma"+"h ttp-refresh" you should find some info.

          And the method using ASP/ASP.NET would be just to serve up whatever page they're supposed to be looking at by keeping tabs on the page they're currently looking at and then no matter what they click serve them up the next allowable page by using the response.redire ct method This has to be called prior to anything being written to the page though, or it won't work.

          Comment

          • sspost
            New Member
            • Sep 2007
            • 15

            #6
            It doesnt work for me...i dont know why
            when i click the back button instead of going to previous page it goes to a page before that ...i dont know whats wrong

            either i want to disable the back button or
            i want when i click the back button all the values enetered by user in the previous page should be their ..which are getting lost...because of which i added my own button and storing all the values from the previous page in session varaibles

            i added the script:
            Code:
            var ie = (window.navigator.appName == "Microsoft Internet Explorer") ? true : false;
                     function setEventByObject(object, event, func){
                     if (!ie){
                     object.addEventListener(event, func, false);
                     } else {
                     object.attachEvent("on" + event, func);
                     }
                     }
                     setEventByObject(window, "unload", exitme);
            
            
                      function jumpforward(){
                      if(window.location.href.indexOf("jumpforward")!=-1) { 
                      history.forward();
                      }
            
                      }
            
                      var block = "true";
            
                      function exitme()
                      {
                      if(block == "true") 
                      {
                      if(window.location.href.indexOf("?")==-1) 
                      {
                       window.location.href += "?jumpforward";
                       } else if (window.location.href.indexOf("jumpforward")==-1)
                       {
                        window.location.href += "&jumpforward";
                       }
                          jumpforward();
                      }
                     }
            but it would fire on all the buttons on the page ...i am not sure whats wrong with it...i am not very gud @ javascript?

            can someone please help figure out whats wrong?

            Comment

            • Shashi Sadasivan
              Recognized Expert Top Contributor
              • Aug 2007
              • 1435

              #7
              javascript control on the browser buttons should be termed as going obsolete.
              As mentioned above, the user might disable javascripts. But you can load the page only if javascript is enabled (which makes sure javascripts will work)

              However, that will still not stop the cause of the web pages being stored on the system, And if theu user has a desktop search (like of live and google desktop) any sensitive information will be indexed....not good at all..

              use the HttpCacheabilit y.NoCache in your pages. (generally i would put that into a master page , including checking is a user is logged in and all those standard things)

              that will tell the browser not to store any web pages sent to it. and so if the back button is pressed, the page dosent exist with the user, so it will never go back. :)

              Comment

              Working...