【问题标题】:disable browser back button javascript禁用浏览器后退按钮 javascript
【发布时间】:2014-04-17 19:22:31
【问题描述】:

我正在使用带有 java 脚本的 MVC 4。

今天我要求在所有浏览器中禁用后退按钮,需要防止在浏览器的后退按钮的帮助下单击注销按钮后返回到上一页(主页)。

帮帮我,谢谢。

【问题讨论】:

    标签: c# javascript asp.net-mvc-4


    【解决方案1】:

    理想情况下,您首先要防止会话历史记录被填充。

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

    【讨论】:

      【解决方案2】:

      可能重复.. 这里之前提到了一个解决方案:

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

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

      【讨论】:

        【解决方案3】:

        你可以如下使用:

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

        【讨论】:

          【解决方案4】:

          试试这个,希望对你有帮助。

          链接: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>
          

          【讨论】:

            【解决方案5】:

            试试这个

            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>
            

            mvc 4 中的 ActionResult 代码示例

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

            【讨论】:

              猜你喜欢
              • 2011-08-06
              • 2010-10-31
              • 2011-04-06
              • 1970-01-01
              • 2017-06-29
              • 2023-03-08
              • 2020-05-15
              相关资源
              最近更新 更多