Disabling Back button on the browser [closed]

2018-12-31 18:12发布

I am writing an application that if the user hits back, it may resend the same information and mess up the flow and integrity of data. How do I disable it for users who are with and without javascript on?

13条回答
心情的温度
2楼-- · 2018-12-31 18:31

Here's a previous post on it: Prevent Use of the Back Button (in IE)

查看更多
临风纵饮
3楼-- · 2018-12-31 18:32

I came up with a little hack that disables the back button using JavaScript. I checked it on chrome 10, firefox 3.6 and IE9:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<title>Untitled Page</title>
<script type = "text/javascript" >
function changeHashOnLoad() {
     window.location.href += "#";
     setTimeout("changeHashAgain()", "50"); 
}

function changeHashAgain() {
  window.location.href += "1";
}

var storedHash = window.location.hash;
window.setInterval(function () {
    if (window.location.hash != storedHash) {
         window.location.hash = storedHash;
    }
}, 50);


</script>
</head>
<body onload="changeHashOnLoad(); ">
Try to hit back!
</body>
</html>
查看更多
萌妹纸的霸气范
4楼-- · 2018-12-31 18:35

It's not possible, sadly. However, consider your applications navigation model. Are you using Post/Redirect/Get PRG Model? http://en.wikipedia.org/wiki/Post/Redirect/Get?

This model is more back button friendly than the Postback model.

查看更多
流年柔荑漫光年
5楼-- · 2018-12-31 18:39

You shouldn't.

You could attach some script to the onbeforeunload event of a page and confirm with the user that's what they want to do; and you can go a bit further and try to disable it but of course that will only work for users who have javascript turned on. Instead look at rewriting the app so you don't commit transactions on each page submit, but only at the end of the process.

查看更多
春风洒进眼中
6楼-- · 2018-12-31 18:40

I strongly urge you to go to heroic lengths to prevent breaking the back button, it is a sure fire way to alienate your users and even made it to No.1 on Jacob Neilsen's Top 10 Web Design Mistakes in 1999.

Perhaps you could consider rather asking the question: "How to avoid breaking the back button for <insert your scenario here>?"

If Scott's answer hits close to the mark, consider changing your flow to the PRG model. If it's something else, then give a bit more detail and see how we can help.

查看更多
爱死公子算了
7楼-- · 2018-12-31 18:40

Find below link

Disable browser back button functionality using JavaScript in asp.net | ASP.Net disable browser back button (using javascript)

http://www.aspdotnet-suresh.com/2011/11/disable-browser-back-button.html

查看更多
登录 后发表回答