How to disable the back button in browser?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mynkow
    New Member
    • Jul 2007
    • 37

    How to disable the back button in browser?

    I accept any other solution for this:
    I have five pages for registrering users. But I don't want users to be able to go back using browser back button. For example: Fill Page1 and go to Page2 then Page3. If user clicks back button I want to redirect him to Page1.
  • vinci
    New Member
    • Aug 2006
    • 62

    #2
    Originally posted by mynkow
    I accept any other solution for this:
    I have five pages for registrering users. But I don't want users to be able to go back using browser back button. For example: Fill Page1 and go to Page2 then Page3. If user clicks back button I want to redirect him to Page1.
    this is possible by using javascript

    Code:
    <SCRIPT language="javascript">
              if(window.history.forward(1) != null)
              window.history.forward(1);
    </SCRIPT>
    just paste this code in the Head tags of your web

    this wont disable the back button instead in just sends forward the page again when the back button is clicked.. it is because the browser saves the previous page and stores its memory in the history, so when the user clicks back and the page was already opened and already stored in history; thats when the condition (if statement) becomes true and forwards the page back...

    enjoy!

    Comment

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

      #3
      Originally posted by mynkow
      I accept any other solution for this:
      I have five pages for registrering users. But I don't want users to be able to go back using browser back button. For example: Fill Page1 and go to Page2 then Page3. If user clicks back button I want to redirect him to Page1.
      One way is to set the httpCachebility to false
      so the client will not store any web history, and the back or the forward buttons wil not work.
      PLace your own back buttons in the web page to redirect the user wherever they are to be redirected

      Hope this helps

      Comment

      • mynkow
        New Member
        • Jul 2007
        • 37

        #4
        This works only for IE. Any solution for Firefox, Opera, Safari and Netscape?
        I tried this in PageLoad method:

        Code:
        Response.Buffer	=	true;
        Response.ExpiresAbsolute=DateTime.Now.AddDays(-10);
        Response.Expires =-15000;
        Response.CacheControl = "no-cache";
        if(Session["SessionID"] == null)
        {
        	Response.Redirect ("Page.aspx");
        }
        Not successful because when I click back button I cannot enter PageLoad Method and receive error page like 404. Seems like the browser is looking for the cache but cannot find it.

        Comment

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

          #5
          Have you tried
          Code:
          Response.Cache.SetCacheability(HttpCacheability.NoCache)
          Also have a look at :
          http://forums.asp.net/t/1060173.aspx

          Comment

          • mynkow
            New Member
            • Jul 2007
            • 37

            #6
            Yes, I tried. Still not working correctly.

            Comment

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

              #7
              Originally posted by mynkow
              Yes, I tried. Still not working correctly.
              got this from one of the forums

              Code:
              response.Cache.SetExpires(DateTime.Now.AddSeconds(  -60));
              response.Cache.SetCacheability(HttpCacheability.No  Cache);
              response.Cache.SetValidUntilExpires(false);
              response.Expires = 0;
              response.Cache.SetNoStore();
              response.AppendHeader("Pragma", "no-cache");

              Comment

              • mynkow
                New Member
                • Jul 2007
                • 37

                #8
                This works but If I have 1 or more postbacks don't.This is not a solution.

                Comment

                • mynkow
                  New Member
                  • Jul 2007
                  • 37

                  #9
                  OK
                  I use this for IE (not very good because you can disable Javascript in browser):

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

                  and for Firefox,Safari, Netscape this code in the PageLoad() Method:

                  if( !Request.Browse r.Browser.ToStr ing().Equals( "IE" ) )
                  {
                  Response.Cache. SetExpires(Date Time.Now.AddSec onds( -60));
                  Response.Cache. SetCacheability (HttpCacheabili ty.NoCache);
                  Response.Cache. SetValidUntilEx pires(false);
                  Response.Expire s = 0;
                  Response.Cache. SetNoStore();
                  Response.Append Header("Pragma" , "no-cache");
                  }

                  This work with IE,Firefox,Safa ri and Netscape but not with Opera.
                  Any solution for Opera?

                  10x Shashi Sadasivan for the help.

                  Comment

                  • kbobce
                    New Member
                    • Jul 2008
                    • 5

                    #10
                    Just insert this within head :

                    <script type="text/javascript">
                    function noBack(){window .history.forwar d()}
                    noBack();
                    window.onload=n oBack;
                    window.onpagesh ow=function(evt ){if(evt.persis ted)noBack()}
                    window.onunload =function(){voi d(0)}
                    </script>

                    It works with IE, Firefox, Safari and Opera using cache of client.

                    Comment

                    • kbobce
                      New Member
                      • Jul 2008
                      • 5

                      #11
                      P.S. history.forward () method never has any parameter ;=)

                      Comment

                      • nmsreddi
                        Contributor
                        • Jul 2006
                        • 366

                        #12
                        Hello Kbobce

                        i have tried by placing the script(what you have posted) in Head part of my web page ,but still it doesn't works any more to do after placing the script

                        Comment

                        • kbobce
                          New Member
                          • Jul 2008
                          • 5

                          #13
                          Hi,
                          Sorry I don't know why there's a space that came inside the script.
                          One should read :
                          window.onpagesh ow=function(evt ){if(evt.persis ted)noBack()}
                          Hoping this'll appear OK.
                          Regards

                          Comment

                          • kbobce
                            New Member
                            • Jul 2008
                            • 5

                            #14
                            Again ! Let's try to write it in another way :
                            <script type="text/javascript">
                            function noBack(){
                            window.history. forward()
                            }
                            noBack();
                            window.onload= noBack;
                            window.onpagesh ow=
                            function(evt){
                            if (evt.persisted) noBack()
                            }
                            window.onunload =
                            function(){
                            void(0)
                            }
                            </script>

                            Comment

                            • kbobce
                              New Member
                              • Jul 2008
                              • 5

                              #15
                              It's OK now.
                              No space inside "noBack"

                              Comment

                              Working...