【发布时间】:2013-05-24 09:05:05
【问题描述】:
当用户使用 HTML5 popstate 处理程序返回浏览器历史记录时,我正在尝试检索滚动位置。
这是我所拥有的:
$(document).ready(function () {
$(window).on('popstate', PopStateHandler);
$('#link').click(function (e) {
var stateData = {
path: window.location.href,
scrollTop: $(window).scrollTop()
};
window.history.pushState(stateData, 'title', 'page2.html');
e.preventDefault();
});
});
function PopStateHandler(e) {
alert('pop state fired');
var stateData = e.originalEvent.state;
if (stateData) {
//Get values:
alert('path: ' + stateData.path);
alert('scrollTop: ' + stateData.scrollTop);
}
}
<a id="link" href="page2.html"></a>
当我返回时,我无法检索 stateData 的值。
我认为这是因为 popstate 正在检索初始页面加载的值,而不是我在单击超链接时推送到历史记录的状态。
如何在返回时获取滚动位置?
【问题讨论】:
标签: javascript jquery html pushstate html5-history