简体   繁体   English

前进按钮在 history.pushState 后不起作用

[英]Forward button not working after history.pushState

I've found how to fix the back button, but the forward button has remained unfix-able.我已经找到了如何修复后退按钮,但前进按钮仍然无法修复。 The url will change but the page doesn't reload, this is what I'm using: 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();

As you can see, I've tried several different things, and the back button works just fine but not the forward button.如您所见,我尝试了几种不同的方法,后退按钮工作正常,但前进按钮无效。

Keep in mind that history.pushState() sets a new state as the newest history state.请记住, history.pushState()将一个新状态设置为最新的历史状态。 And window.onpopstate is called when navigating (backward/forward) between states that you have set.在您设置的状态之间导航(向后/向前)时会调用window.onpopstate

So do not pushState when the window.onpopstate is called, as this will set the new state as the last state and then there is nothing to go forward to.所以当window.onpopstate被调用时不要pushState ,因为这会将新状态设置为最后一个状态,然后就没有什么可前进的了。

Complete solution working with multiple clicks on back and forward navigation.多次单击后退和前进导航的完整解决方案。

Register globally window.onpopstate event handler, as this gets reset on page reload (and then second and multiple navigation clicks don't work):全局注册window.onpopstate事件处理程序,因为它会在页面重新加载时重置(然后第二次和多次导航点击不起作用):

window.onpopstate = function() {
    location.reload();
};

And, update function performing AJAX reload (and, for my use-case replacing query parameters):并且,执行 AJAX 重新加载的更新函数(以及,对于我的用例替换查询参数):

function update() {
    var currentURL = window.location.href;
    var startURL = currentURL.split("?")[0];

    var formParams = form.serialize();

    var newURL = startURL + "?" + formParams;
    var ajaxURL = startURL + "?ajax-update=1&" + formParams;

    $.ajax({
        url: ajaxURL,
        data: {id: $(this).attr('id')},
        type: 'GET',
        success: function (dataRaw) {
            var data = $(dataRaw);

            // replace specific content(s) ....

            window.history.pushState({path:newURL},'',newURL);
        }
    });
}

I suggest reading about navigating browser history and the pushState() method here .我建议在此处阅读有关浏览浏览器历史记录和pushState()方法的内容。 It explicitly notes that pushState() by itself will not cause the browser to load a page.它明确指出pushState()本身不会导致浏览器加载页面。

As far as the forward button not working, once you call pushState() the browser is (conceptually) at the last (latest) page of the history, so there is no further page to go "forward" to.至于前进按钮不起作用,一旦您调用pushState()浏览器(概念上)位于历史记录的最后(最新)页面,因此没有进一步的页面可以“前进”到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM