【发布时间】:2015-04-12 13:35:00
【问题描述】:
我正在构建一个 Web 应用程序,我需要在其中防止浏览器历史记录中的后退导航。在 StackOverflow 中搜索线程后,我发现:
<!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 the back button!
</body>
</html>
参考Disable browser's back button
我没有更改 JavaScript 中的任何内容。但是,在我的代码中使用它之后,我在访问页面时发现了另一个问题。网页自动滚动到顶部,禁止查看页面底部,同时还禁用了一些其他功能。
有趣的是,当我手动点击刷新按钮时,页面没有显示此症状。我不知道是什么导致了这个问题。
看,我的基本要求是阻止用户访问前一页。如果有任何其他选择,那也将受到欢迎。
请帮我解决这个问题。提前致谢。
【问题讨论】:
-
由于 Javascript 在客户端运行,如果他们愿意,他们可以实时编辑您的脚本,您无法有效地阻止用户使用浏览器的内置功能。
标签: javascript html browser-history