使用 onpopstate 和 Balupton 的 History.js 来返回
我的目标是存储网站的当前状态,以便我可以通过按浏览器后退按钮返回到该状态。目前,我已经使用 HTML5 本机函数使用 pushState 和 popState 实现了此目的,如下所示:
<script type="text/javascript">
$("a").click(function(e) {
$("#element").load($(this).attr('href') + ' #otherElement');
$("html, body").animate({ scrollTop: 0 }, 0);
previous_page = location.href;
window.history.pushState({page: $(this).index()}, $(this).html(), $(this).attr('href'));
e.preventDefault();
return false;
});
window.popupstate = function(e) {
location.reload();
}
</script>
这段代码在 Firefox 4 中按预期工作。但是,在 Chrome 中,页面会永久重新加载。因此,我想使用 Balupton 的 History.js 库在所有浏览器中运行代码片段,但我发现 Balupton 页面上的示例不是很有帮助。
我已经尝试了以下方法(不起作用):
var History = window.History;
History.pushState({state:'root'},'root',$(this).attr('href'));
$("a").click(function(e) {
$("#element").load($(this).attr('href') + ' #otherElement');
$("html, body").animate({ scrollTop: 0 }, 0);
History.pushState({state:$(this).attr('href')},$(this).attr('href'),$(this).attr('href'));
return false;
});
History.Adapter.bind(window,'statechange',function(){
var State = History.getState(); // Note: We are using History.getState() instead of event.state
History.log('statechange:', State.data, State.title, State.url);
if (State.title == 'root') {
History.back();
location.reload();
}
});
谢谢。
my objective is to store the current state of a web site, such that I can return to that state via pressing the browsers back button. Currently, I've achieved this using pushState and popState using the HTML5 native functions as follows:
<script type="text/javascript">
$("a").click(function(e) {
$("#element").load($(this).attr('href') + ' #otherElement');
$("html, body").animate({ scrollTop: 0 }, 0);
previous_page = location.href;
window.history.pushState({page: $(this).index()}, $(this).html(), $(this).attr('href'));
e.preventDefault();
return false;
});
window.popupstate = function(e) {
location.reload();
}
</script>
That piece of code works as expected in Firefox 4. However, in Chrome, the page reloads permanently. Thus, I would like to use Balupton's History.js library to run the code snippet in all browser, but I found the example on Balupton's page not very helpful.
I've tried the following (which does not work):
var History = window.History;
History.pushState({state:'root'},'root',$(this).attr('href'));
$("a").click(function(e) {
$("#element").load($(this).attr('href') + ' #otherElement');
$("html, body").animate({ scrollTop: 0 }, 0);
History.pushState({state:$(this).attr('href')},$(this).attr('href'),$(this).attr('href'));
return false;
});
History.Adapter.bind(window,'statechange',function(){
var State = History.getState(); // Note: We are using History.getState() instead of event.state
History.log('statechange:', State.data, State.title, State.url);
if (State.title == 'root') {
History.back();
location.reload();
}
});
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经为我的问题找到了有效的解决方案。由于我在替换整个 DOM 路径时丢失了绑定,因此我只需要隐藏/显示元素,如下所示:
该解决方案还不太理想。
I've found a working solution for my problem. Since I am loosing the bindings while replacing an entire DOM path, I need just to hide/show the elements as follows:
The solution is not quite ideal, yet.