我已经找到了如何修复后退按钮,但前进按钮仍然无法修复。URL将会改变但页面不会重新加载,这是我正在使用的方法:
$('.anchor .wrapper').css({
'position': 'relative'
});
$('.slanted').css({
'top': 0
});
// Do something after 1 second
$('.original-page').html('');
var href = '/mission.html'
console.log(href);
// $('#content-div').html('');
$('#content-div').load(href + ' #content-div');
$('html').scrollTop(0);
// loads content into a div with the ID content_div
// HISTORY.PUSHSTATE
history.pushState('', 'New URL: ' + href, href);
window.onpopstate = function(event) {
location.reload();
};
// response.headers['Vary'] = 'Accept';
// window.onpopstate = function (event) {
// alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
// location.reload();
// response.headers['Vary'] = 'Accept';
// };
// $(window).bind('popstate', function () {
// window.onpopstate = function (event) {
// window.location.href = window.location.href;
// location.reload();
// };
e.preventDefault();
你可以看到,我尝试了几种不同的方法,后退按钮可以正常工作,但是前进按钮无法操作。
history.pushState
的调用从函数中分离出来,这样当我使用window.onpopstate
时就不会调用history.pushState
了。 - Timothy Fisher