Thursday, 6 April 2017

How to disable browser back button

How to disable browser back button using javascript

Hi All, I'm explaining to disable all browser back button using javascript in MVC 4

Today's i have a requirement to disable to back button in all browsers for this need to prevent to go-back to previous page(home page) after click on log-out button

1st step, we write to javascript code to all the pages where we need to disable the browser back button.

  function DisableBackButtonAllBrowsers() {
         window.onload = DisableBackButtonAllBrowsers();
         window.onpageshow =function (evts) {if (evts.persisted) DisableBackButtonAllBrowsers();};
         window.onunload =function () { void (0) };

In the 2nd step, we  write the code for response to ActionResult for log-out in MVC 4. i.e.

/// <summary>
/// Logs user out and renders login <see cref="View"/>
/// </summary>
/// <returns>Login <see cref="View"/></returns>
    public ActionResult Logout()
            //Disable back button In all browsers.
            return View("Login");

Note: The above code is working most of all browsers.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.