0

In my web application I have a login first and then if the login is successful I redirect the user to their account page. I want to disable browser back button (shown in below image) when he/she is on the account page after login.

disable back button

I don't want to let user to go back to login page. I tried all these window.history.forward stuff from google, but I want to disable this browser back button please help me on this. I am very much stuck here.

Adam Zuckerman
  • 1,633
  • 1
  • 14
  • 20
Vivek Parikh
  • 627
  • 8
  • 18
  • 31
  • What is the reason to do that? Let's assume there is a back button, so what? – zerkms Mar 29 '14 at 07:34
  • i dont want to by history.forward method because it does that for all other pages.is it possible to do this only from a particular previous page. i mean suppose i apply this code on default.aspx but it sould apply this only from a particular previous page.not for all – Vivek Parikh Mar 29 '14 at 07:37
  • @zerkms i dont want to let user to go to login page... – Vivek Parikh Mar 29 '14 at 07:41
  • 1
    @Vivek Parikh: what if a user just types the login page url? Why not you just redirect from a login page instead of denying what shouldn't be? – zerkms Mar 29 '14 at 07:58

2 Answers2

2

Normally you can NOT disable browser back button but there is some methods that lets you to play around this limitation just this article explains it.

However I see that there is a better way to solve your problem. You can use same page for content and login, and page session to avoid change back button functionality. You can do some thing like:

if(user has valid session)
   include (account_page)
else if (login form has been submitted)
   if (valid user)
     include (account_page)
     create new session for this user
    else 
       include (login_page)
else
   include (login_page)
Polla A. Fattah
  • 801
  • 2
  • 11
  • 32
0

Usually it's counter-productive to fight the browser behavior, instead you should simply redirect the client back to another page if they visit the account page after they have already logged in.

If you are using MVC, this is easy to do by returning a new RedirectAction object in the Index action within the AccountController (or wherever you are displaying the account page)

public ActionResult Index()
{
    if (User.Identity.IsAuthenticated)
        return RedirectToAction("index", "home");

    ... do your other code here if they are not authenticated
}