2014-03-13 237 views
1

我正在使用MVC 4与Java脚本。禁用浏览器后退按钮javascript

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

帮助我谢谢。

回答

3

试试这个

的JavaScript代码示例

JavaScript代码的地方,我们需要禁用浏览器的后退按钮上的所有网页。在MVC 4

代码响应的ActionResult用于注销在MVC 4即

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

谢谢。适用于我 – user3414212

0

试试这个

<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的代码示例,可它会帮助你。

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

您可以如下使用:

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

最好要防止您的会话历史,从在第一时间得到填充。

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