简体   繁体   English

带后退和前进按钮的history.pushState

[英]history.pushState with back and forward buttons

I need to make back and forward buttons working with this ajax method 我需要使前进和后退按钮与此ajax方法一起使用
This code working good and the URL on browser changing as it should be 此代码可以正常工作,并且浏览器上的URL应当按原样进行更改
but when you click on back and forward buttons, nothing happening 但是当您单击后退和前进按钮时,没有任何反应

(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);

})();

I had to deal with this today. 我今天不得不处理这个。 The following works for me (tested on four browsers in Win 7 so far)... 以下对我有用(到目前为止,已在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);

it is because your history is empty. 这是因为您的历史记录是空的。 For this you need to use 为此,您需要使用

 history.pushState();

After that my buttons start working 之后,我的按钮开始工作

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

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