我正在構建一個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的工作是在客戶端,他們可以編輯你的腳本生活,如果他們覺得它跑,你不能有效地利用瀏覽器的內置阻止用戶功能。 – Nit