当我使用
以下是一小段代码,用于示例此“问题”:
window.history.pushState
更改URL时,浏览器的历史回退按钮不能自动重新加载页面。为什么页面不能自动重新加载?我可以改变这种行为吗?以下是一小段代码,用于示例此“问题”:
<html>
<head>
<title>Location test</title>
<script>
function load() {
var value = window.location.search.substr(1);
document.getElementById('myInput').value = value;
document.title = 'Location test - ' + value;
}
function set(el) {
window.history.pushState('', '', window.location.pathname + '?' + el.value);
document.title = 'Location test - ' + el.value;
}
</script>
</head>
<body onload="load();">
<input id="myInput" type="text" onkeyup="set(this);">
</body>
</html>
只需在文本框中输入内容,浏览器历史记录将相应更新。单击浏览器历史记录的返回按钮不会刷新页面,您需要手动刷新。
我通过在网站上插入自己的“返回按钮”来解决了这个问题,它运行window.history.back(); location.reload();
。但如果普通浏览器的“后退按钮”也能起作用,那我会很高兴。