【问题标题】:history.pushState with back and forward buttonshistory.pushState 带有后退和前进按钮
【发布时间】:2013-05-08 07:45:36
【问题描述】:

我需要使用这个 ajax 方法制作后退和前进按钮
这段代码运行良好,浏览器上的 URL 应该会发生变化
但是当你点击后退和前进按钮时,什么都没有发生

(function(){

    function clickNav(e){
        e.preventDefault();
        var href = $(this).attr('href');
        history.pushState(null, null, href);

        $.ajax({
            url: href,
            dataType: 'html',
            cache: false
        }).done(function(html){
            var $html = $(html);

            $('title').html($html.filter('title').text());
            $('#content').replaceWith($html.find('#content'));
            $('#nav').replaceWith($html.find('#nav'));

            $('#nav li a').on('click',clickNav);
        });

    }
    $('#nav li a').on('click',clickNav);

})();

【问题讨论】:

标签: javascript ajax html jquery


【解决方案1】:

我今天不得不处理这个问题。以下对我有用(到目前为止在 Win 7 中的四个浏览器上测试过)...

//CHECK FOR ADDRESS BAR CHANGE EVERY 500ms
var wwcint = window.setInterval(function(){
    if(window.last_location_href == undefined) {
        window.last_location_href = document.location.href;
    } else if(window.last_location_href != document.location.href) {
        window.last_location_href = document.location.href;

        //DO WHATEVER YOU NEED TO DO TO LOAD THE CORRECT CONTENT
        someFunctionToLoadTheContent(document.location.href);
    }
}, 500);

【讨论】:

    【解决方案2】:

    这是因为你的历史是空的。为此,您需要使用

     history.pushState();
    

    之后我的按钮开始工作

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    相关资源
    最近更新 更多