history.pushstate jquery back button not loading prev content

冷暖自知 提交于 2019-12-11 03:15:43

问题


I am trying to implement history.pushstate with jQuery as described

This is the following code I have:

container = $(".loadContent");
currentItem = null;
$(".list-group a").each(function (index) {
    var item = $(this);
    if (item.hasClass("active")) {
        currentItem = item;
    }
    item.on("click", function(e) {
        e.preventDefault();
        history.pushState({}, '', $(this).attr("href"));
        $(".heightDiv").css("height", divheight);
        if (currentItem == item) return;
        $(".content").empty();
        $(".loading").css("height", divheight).show();
        url = this.href + " .content";
        if (currentItem) currentItem.removeClass("active");
        currentItem = item.addClass("active");
        container.load(url, function(){$(".loading").hide();});
        return false;
    });
});

loadPage = function(href) {
  container.load(href + " .content");
};

$(window).on("popstate", function(e) {
  if (e.originalEvent.state !== null) {
    loadPage(location.href);
  }
});

The address bar it is picking up the correct address, however when I hit the back button, the address bar gets back correctly but the previous content it is not loaded

来源:https://stackoverflow.com/questions/26277054/history-pushstate-jquery-back-button-not-loading-prev-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!