简体   繁体   English

javascript pushState / onpopstate不能与页面上的常规链接结合使用

[英]javascript pushState / onpopstate won`t work in combination with normal links on page

I am fighting here with pushState and onpopstate events. 我在这里通过pushState和onpopstate事件进行战斗。 Back forward buttons in browser works as expected ater i browse trough AJAX (a#itemPages) links. 在浏览器通过AJAX(a#itemPages)链接浏览时,浏览器中的后退按钮可以正常工作。 But if i click on normal links afterward and i go back by back button in browser, i am not able to move forward to normal links which should be in bworser history. 但是,如果我之后单击普通链接,然后按浏览器中的“后退”按钮返回,我将无法前进到应该在浏览器历史记录中的普通链接。 I Am still able to move back and forward between "ajaxified" links, but normal links which should exist later in browser history simply disappear (forward button in FF slides out like there are no other items in history after actual item). 我仍然可以在“ ajaxified”链接之间来回移动,但是应该在浏览器历史记录中稍后存在的普通链接会消失(FF中的“前进”按钮会滑出,就像历史记录中没有其他条目一样)。 Moreover, last clicked "ajaxified" link present in history two times. 此外,最后单击的“ ajaxified”链接在历史记录中出现了两次。

$('body').on('click', 'a#itemPages', function(event){
    $(#plc).hide().fadeIn('slow');
    $.get($(this).attr('rel'), function(data) {
        $(#plc).html(data);
    });
    var stateObj = { URL: $(this).attr('rel') };
    history.pushState(stateObj, null, $(this)[0].href);
    return false;
});

window.onpopstate = function(e) {
    if (e.state !== null) {
        var prevstate = e.state.URL;
        $('#plc').hide().fadeIn('slow');
        $.get(prevstate, function(data) {
            $('#plc').html(data);
        });
    }
};

sorry guys. 对不起大家。 there were another piece of the JS on the index page which manipulated history too. 索引页面上还有另一段JS,也可以操纵历史记录。 this brings the problem. 这带来了问题。 closing this issue. 解决这个问题。

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

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