【发布时间】:2014-09-28 19:32:59
【问题描述】:
我正在尝试在页面首次加载时存储第一页信息。这样当我点击第二页时,我可以找回第一页的信息。
(Initial Page -> Page2 -> Initial Page)
经过几个小时的工作,我决定存储一个名为 firstLoad 的全局变量,并使用 replaceState 而不是 pushState。现在可以了。但是有一个问题,不能这样:
Initial Page -> Page2 -> Page3 -> Page2
当Page 3回到Page 2时,控制台显示:
GET http://localhost/[object%20Object] 404 (Not Found)
我用谷歌搜索过,但找不到任何合适的教程来处理这个问题,他们中的大多数人建议使用 history.js,但我正在尝试先了解它是如何工作的。任何帮助将不胜感激!
<script>
$(function(){
var firstLoad = 1;
$('a').on('click', function(e){
e.preventDefault();
var path = this.href;
var currentPath = window.location.pathname;
loadContent(path);
if(firstLoad==1){
console.log(firstLoad);
console.log(currentPath);
firstLoad=0;
history.replaceState(currentPath, '', '');
history.pushState({},'',path);
}else{
console.log('Not first load')
history.pushState(path,'',path);
}
});
//Pop State
$(window).on('popstate', function(e){
var state = e.originalEvent.state;
if(e.originalEvent && state){
loadContent(state);
}
});
//Load Content
function loadContent(path){
$.get(path, function(data){
$('#result').html(data);
});
};
});
</script>
【问题讨论】:
标签: javascript jquery html history.js html5-history