【发布时间】:2013-10-31 16:39:34
【问题描述】:
我正在使用 History.js(jquery.history.js v1.8b2)。 Chrome 版本 30.0.1599.101 m.
我不知道如何让 History.js 以我期望的方式工作。
我有一个包含两个链接的页面。第一个模拟ajax 操作。它只是将页面中<h2> 中的文本从“开始”更改为“2”。第二个链接只是指向 google.com。
这是我的工作:
- 在 chrome 中清除缓存
- 加载页面
- 单击第一个链接(我调用 pushState,发生 statechange,然后我将文本更新为“2”)
- 点击 google.com 链接(转到 google)
- 单击浏览器的返回按钮。
单击后退按钮后,我希望调用 statechange 处理程序,然后我会获取我的状态对象,以便将文本恢复为“2”。
但是处理程序没有被调用。所以我的页面留下了文本“开始”(浏览器中的缓存页面)。有趣的是,该 url 设置为我在 pushState 调用中推送的“2”版本。
我错过了什么吗?这是我的代码:
<h2 id="state">start</h2>
<a id="2" class="button" href="#">Goto State 2</a>
<a href="http://google.com">Goto google</a>
<script type="text/javascript">
$(function() {
History.Adapter.bind(window, "statechange", function() {
console.log("statechange called");
var state = History.getState();
if (state.data.mystate) {
// Restore the old state
$("#state").text(state.data.mystate);
} else {
// Start state
$("#state").text("start");
}
});
$(".button").click(function(e) {
// push the state we're transitioning to
var newState = $(this).attr("id");
History.pushState(
{ mystate: newState },
"at state " + newState,
"http://localhost:50494/PushState?state=" + newState);
// statechange will be called now, and we'll update the page from our newState
e.preventDefault();
});
});
</script>
【问题讨论】:
标签: html history.js