Duplicate History enteries using History.pushState

2020-06-23 09:36发布

问题:

I am having a strange problem in Chrome and Safari using History.js from https://github.com/browserstate/history.js/

If I click on several different ".node" items and then look at my browsers history, I have duplicate entries for each click and need to his the back button twice to go back the the last "real" state. I added a console.log(id) in and can confirm that the event is only firing the one time.

In Firefox it works as expected. I am only seeing the duplicate history items in Chrome and Safari.

$( "body" ).delegate( ".node" , "click", function(){
    id = $(this).data('id');
    History.pushState({"Foo":"Bar","ID":id}, "Title for "+ id, "/item-"+id);
});

回答1:

I was having a similar problem to this in Chrome (wasn't happening in IE and Firefox) and it turned out the extra history entries weren't being added by pushState but happened when I altered the src attribute on iframes via javascript.

Even though they were loading from a remote site (Vimeo in this case) they were showing up in the back/forward button's history as duplicates of the current page and I'd have to click back multiple times.

I've not tested this further so I don't know if this is unique to iframes or if it could be caused by altering any elements src attributes.