1

I am using MVC 4 with java script.

Today's i have a requirement to disable to back button in all browsers, need to prevent to go-back to previous page(home page) after click on log-out button with the help of back button of browsers.

Help Me thanks.

5 Answers 5

3

Try this

JavaScript Code Sample

javascript code to all the pages where we need to disable the browser back button.

<script type="text/javascript">    

   //Disable Back Button In All Browsers.
        function DisableBackButtonAllBrowsers() {
            window.history.forward()
        };
         DisableBackButtonAllBrowsers();
        window.onload = DisableBackButtonAllBrowsers;
         window.onpageshow = function (evts) { if (evts.persisted) DisableBackButtonAllBrowsers(); }; 
        window.onunload = function () { void (0) };
    </script>

ActionResult code sample in mvc 4

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.
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
            Response.Cache.SetNoStore();

            FormsAuthentication.SignOut();
            return View("Login");
     }
Sign up to request clarification or add additional context in comments.

Comments

0

Try this, may it will help you.

Link: http://hightechnology.in/how-to-disable-browser-back-button-in-asp-net-using-javascript/

Javascript Code:

<script type = "text/javascript" >
function preventBack(){window.history.forward();}
setTimeout("preventBack()", 0);
window.onunload=function(){null};
</script>

Comments

0

You can use as below :

<script type="text/javascript" language="javascript">
    function DisableBackButton() {
    window.history.forward()
    }
    DisableBackButton();
    window.onload = DisableBackButton;
    window.onpageshow = function(evt) { if (evt.persisted) DisableBackButton() }
    window.onunload = function() { void (0) }
</script>

Comments

0

Possible duplicate.. there is a solution mentioned befor here:

set cachebality to noCache.. 
Page.Response.Cache.SetCacheability(HttpCacheability.NoCache)

Disable browser "back" button
Disable browser's back button

Comments

0

Ideally you want to prevent your session history from getting populated in the first place.

location.replace(url)

From: https://developer.mozilla.org/en-US/docs/Web/API/Location/replace

The Location.replace() method replaces the current resource with the one at the provided URL. After using replace() the current page will not be saved in session History, meaning the user won't be able to use the back button to navigate to it.

If the session history is empty, the back button is disabled. If your app depends heavily on form submission, jQuery makes it easy to convert form variables to a query string for use with location.replace.

function submitForm() {
    // this eliminates issues with the back button
    window.location.replace('?' + jQuery.param(jQuery('form').serializeArray()));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.