Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何防止浏览器在散列更改时滚动到顶部?_Javascript_Html_Browser History - Fatal编程技术网

Javascript 如何防止浏览器在散列更改时滚动到顶部?

Javascript 如何防止浏览器在散列更改时滚动到顶部?,javascript,html,browser-history,Javascript,Html,Browser History,我正在构建一个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>

我正在构建一个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>

无标题页
函数changehasonload(){
window.location.href+=“#”;
setTimeout(“changehashtreach()”,“50”);
}
函数changehash(){
window.location.href+=“1”;
}
var storedHash=window.location.hash;
window.setInterval(函数(){
if(window.location.hash!=storedHash){
window.location.hash=storedHash;
}
}, 50);
试着按下后退按钮!
参考文献

我没有更改JavaScript中的任何内容。然而,在我的代码中使用了它之后,我在访问页面时发现了另一个问题。网页将自动滚动到顶部,禁止查看页面底部,同时禁用其他一些功能

有趣的是,当我手动点击刷新按钮时,页面没有显示这种症状。我不明白是什么导致了这个问题

看,我的基本要求是阻止用户访问上一页。如果还有其他选择的话,那也是受欢迎的


请帮我把这个修好。提前感谢。

您基本上是每50毫秒重新加载一次页面

window.location.href += "#";
window.location.href += "1";
因为location.href属性更改时浏览器会重新加载。重新加载后,您会再次调用该方法

因此,站点每次都会再次显示在顶部


您可以在一个变量中生成散列,并使用它进行比较。

我修改了代码。现在它可以在所有现代浏览器,IE8及以上版本中使用

var storedHash = window.location.hash;
function changeHashOnLoad() {
    window.location.href += "#";
    setTimeout("changeHashAgain()", "50");
}

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

function restoreHash() {
    if (window.location.hash != storedHash) {
        window.location.hash = storedHash;
    }
}

if (window.addEventListener) {
    window.addEventListener("hashchange", function () {
        restoreHash();
    }, false);
}
else if (window.attachEvent) {
    window.attachEvent("onhashchange", function () {
        restoreHash();
    });
}
$(window).load(function () { changeHashOnLoad(); });

由于Javascript是在客户端运行的,如果用户愿意,他们可以实时编辑脚本,因此无法有效阻止用户使用浏览器的内置功能。