簡體   English   中英

禁用瀏覽器后退按鈕javascript

[英]disable browser back button javascript

我正在使用MVC 4和java腳本。

今天我要求在所有瀏覽器中禁用后退按鈕,需要在瀏覽器后退按鈕的幫助下點擊退出按鈕后阻止返回上一頁(主頁)。

幫幫我謝謝。

嘗試這個

JavaScript代碼示例

javascript代碼到我們需要禁用瀏覽器后退按鈕的所有頁面。

<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代碼示例在mvc 4中

用於在MVC 4中注銷的ActionResult響應代碼

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

試試這個,它可以幫到你。

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

Javascript代碼:

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

您可以使用如下:

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

可能重復..有一個解決方案提到這里:

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

禁用瀏覽器“后退”按鈕
禁用瀏覽器的后退按鈕

理想情況下,您希望首先防止會話歷史記錄填充。

location.replace(url)

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

Location.replace()方法將當前資源替換為提供的URL處的資源。 使用replace()后,當前頁面將不會保存在會話歷史記錄中,這意味着用戶將無法使用后退按鈕導航到該頁面。

如果會話歷史記錄為空,則禁用后退按鈕。 如果您的應用程序在很大程度上依賴於表單提交,那么jQuery可以很容易地將表單變量轉換為查詢字符串以與location.replace一起使用。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM