我第一次玩历史对象。我有以下代码,我一直在 chrome 中测试:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
window.addEventListener('popstate', function(event) {
console.log('popstate fired!');
console.log(event.state);
});
function showbox()
{
document.getElementById('box').style.display = 'block';
history.pushState({somethinggoeshere:'but what?'}, 'I put text here, but how do i verify the browser has received it?', '?showbox=true');
}
</script>
</head>
<body>
<a onclick="showbox();">Show box</a>
<div id="box" style="background:red; height:100px; display:none;"></div>
</body>
</html>
当我单击链接显示框时,会出现一个红色框。当我单击 chrome 浏览器窗口中的后退按钮时,然后查看 console.log,event.state 为空。为什么它是空的?我不是用 history.pushState 填充这个吗?