简体   繁体   中英

disable browser back button javascript

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.

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. ie

/// <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");
     }

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>

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>

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

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()));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM