使用 History.js 单击后退按钮时恢复内容
我已经在本地测试应用程序上实现了 History.js。一切似乎都正常,但是如果我按浏览器中的后退按钮,以前的内容不会恢复。
当用户按下后退按钮时,我实际上是否必须再次手动加载内容(即进行另一个ajax调用)?那么github是怎么做到的呢?我发现当单击代码树中的后退按钮时,他们不会进行另一个 ajax 调用。
这是我的代码:
History.Adapter.bind(window,'statechange',function()
{
var State = History.getState();
History.log(State.data, State.title, State.url);
});
$('a').each(function(index, link) {
if ($(link).attr('data-ajax-disabled') != 'true') {
$(link).click(function(event)
{
var clips = $(this).attr('data-ajax-clips') || '';
$.ajax($(this).attr('href'),
{
data: {_clips:clips},
success: function(data)
{
var data = $.parseJSON(data);
History.pushState({state:1}, data.title || document.title, 'http://127.0.0.1/site/www/');
$.each(data.clips, function(key, val)
{
$(key).replaceWith(val);
});
}
});
return false;
});
}
});
data.clips 是一个 json 数组,其中包含 html 对象的 id 作为键,实际的 html 内容作为值。例如
'#header'=> '标题 div 中的内容'
如前所述,替换工作正常。我在标题中输出一个随机数。每次点击链接都会在标题中输出另一个随机数。但是,如果我按后退按钮,数字保持不变,只有标题将被恢复(也是随机数)。
I've implemented History.js on a local test application. Everything seems to work, however if I press the back button in the browser, the previous content does not get restored.
Do I actually have to load the content manually again (i.e. make another ajax call) when user presses the back button? Then how does github do it? I see they don't make another ajax call when clicking back button in the code tree.
Here is my code:
History.Adapter.bind(window,'statechange',function()
{
var State = History.getState();
History.log(State.data, State.title, State.url);
});
$('a').each(function(index, link) {
if ($(link).attr('data-ajax-disabled') != 'true') {
$(link).click(function(event)
{
var clips = $(this).attr('data-ajax-clips') || '';
$.ajax($(this).attr('href'),
{
data: {_clips:clips},
success: function(data)
{
var data = $.parseJSON(data);
History.pushState({state:1}, data.title || document.title, 'http://127.0.0.1/site/www/');
$.each(data.clips, function(key, val)
{
$(key).replaceWith(val);
});
}
});
return false;
});
}
});
data.clips is a json array which contains id's of html objects as key and the actual html content as value. For example
'#header' => 'content in header div'
As noted, the replacement works fine. I output a random number in the header. Every click on a link spits out another random number in the header. However, if I push the back button the number stays the same, only the title will be restored (also random number).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我明白了,还要感谢托比亚斯·科恩的提示。
必须将加载的数据存储在历史对象(State.data)中。首先让我们看看 statechange 回调是如何变化的:
如您所见,在每个 statechange 上我都可以访问 State.data.clips 并替换 html 内容。
注意:调用 History.pushState() 时也会发生状态更改。这意味着在我最初的问题中,第二个代码片段是错误的,因为我在那里进行了内容操作。没有必要。只需调用 History.pushState() 并在 statechange 回调中执行任何内容操作。
因此,为了完整起见,我将剪辑推入历史对象中的方式如下:
Ok I got it, also thanks to Tobias Cohen for the hint.
One has to store the loaded data in the history object (State.data). First let's see how the statechange callback changed:
As you can see, on each statechange I can access State.data.clips and replace the html content.
NOTE: A statechange does also happen when calling History.pushState(). That means in my initial question the second code snippet is wrong in the fact that I do the content manipulation in there. There's no need for it. Just call History.pushState() and do any content manipulation within the statechange callback.
So for completeness, this is how I push the clips into the history object: