Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am using history.js to handle back button. In history.js statechange is firing whenever i do a pushstate. Why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
273 views
Welcome To Ask or Share your Answers For Others

1 Answer

Wanted to add, yes this is the expected behaviour of History.js. At the same time there are more discussions that critize this behaviour as it is not the W3C standard and does create some confusion.

In short, to answer your question: In the History.js pushState() function is a call to statechange at the end.

Upside of this solution is that you can just change (push) your new state and let the onstatechange()-function handle the transition. Downside is that you are not able to handle exceptions/or have to write them into the onstatechange event-handler.

I personally prefer the W3C way of handling this, as you can distinguish between back/forward button and pushState. The History.js maintainers are working on an internal flag solution, that enables you to change this behaviour:

Notice how the above calls [pushstate-calls] trigger statechange events, if for some reason you do not want this to happen then inside your statechange handler you can use the following:

if ( History.getState().internal ) { return; }

*This feature is currently in development and can only be used with the 'dev' version of History.js! Hope this will help some other people in the future :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...