【发布时间】:2012-02-16 16:46:51
【问题描述】:
使用 HTML5 History API,我创建了如下内容:
(function (d, w) {
$(function () {
$("#indicator").hide();
if (typeof history.pushState !== 'undefined') {
$("#citylinks a").click(function (e) {
history.pushState({ path: this.href }, '', this.href);
act(this.href);
e.preventDefault();
});
w.onpopstate = function (event) {
act(d.location);
};
}
function act(location) {
$("#results").hide();
$("#indicator").show();
$.getJSON(location, function (data) {
$("#results").html(data.result);
$("#results").fadeIn();
$("#indicator").hide();
});
}
});
})(document, window);
完整的代码在这里:
我在这里遇到的问题是我不想在页面第一次来自服务器时触发 w.onpopstate 事件。
我想这样做的原因是我想在服务器端填充页面,我不希望客户端代码这样做。如果我删除 w.onpopstate 事件,我将无法捕获 history.go() 事件。
有没有办法解决这个问题?
【问题讨论】:
标签: javascript jquery asp.net-mvc html browser-history