我试图弄清楚History.js
,如果你去下面的代码可以工作,index.html
但如果你尝试index.html?info
直接访问,它会显示索引内容。当有人试图直接访问时,您应该如何检测状态index.html?info
?另外,我也不确定我是否正确地执行初始状态。
索引.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>title</title>
<script src="js/jquery-1.10.2.min.js"></script>
<script src="js/jquery.history.js"></script>
</head>
<body>
<div id="content"></div>
<script>
History.Adapter.bind(window, 'statechange', function() {
var id = History.getState().data.id;
if (id == "index") {
$('#content').html('<button id="btn" type="button">get info</button>');
$('#btn').click(function() {
History.pushState({ id: "info" }, '', '?info');
});
}
if (id == "info") {
$('#content').html('here is some info');
}
});
History.pushState({ id: "index" }, '', '?index'); //do this otherwise nothing loads on first run
</script>
</body>
</html>