history.js
history.js copied to clipboard
No statechange event when both path and hash are changed via pushState
Hello,
I believe I found a bug in history.js. When you push a state that changes both the path and the hash part of the location, no statechange event is triggered.
Fiddledydoo: http://jsfiddle.net/yBN6j/
Additional info:
- the fiddle doesn't work in opera 12.16, for some reason jQuery 1.10.1 throws a security exception; when jQuery is switched to version 1.9.1 the fiddle runs okay, but the issue is still present
- firefox 23.0.1 has this problem as well
- chrome 29.0.1547.76 too
- can't test safari as I don't have it
I am also having this exact same problem, in IE10, chrome, pretty much every browser. A named anchor will completely fail to work. Example:
<a href="/somepage.html#content">Go To Content</a>
will not fire the statechange event?
:+1:
happens here as well. Currently debugging it. What I found out so far (using debug = true):
- calls pushState
- sets busy to true
- stores the state (i.e. setting the new location)
- triggers
onpopstate - logs
History.onPopState: traditional anchor ["foobar"] - Triggers anchorchange
- busy to false
... so the issue is: onPopState doesn't check if the current href is the same. It just checks if there's an anchor. And as far as I've seen, it can't really check if the current href is the same, because the location is already updated.
I'm currently experimenting with the code to see what might fix it.. I have an experimental fix for now, would be cool if someone of you could test it as well:
// Reset the double check
History.doubleCheckComplete();
+ var currentPath = document.location.pathname,
+ newPath = History.getShortUrl(History.getState().cleanUrl);
+
// Check for a Hash, and handle apporiatly
currentHash = History.getHash();
- if ( currentHash ) {
+ if ( currentHash && currentPath === newPath ) {
// Expand Hash
currentState = History.extractState(currentHash||document.location.href,true);
if ( currentState ) {
This is around (Line 1695)[https://github.com/browserstate/history.js/blob/master/scripts/uncompressed/history.js#L1695]. The only problem I have currently is that the browser doesn't scroll to the anchor :/
@mweibel thanks, that fix works for me. Unhelpfully, due to the way the site works, my fix for scrolling the page to the appropriate place is buried in our code.
We had this in place for ~1 year and it worked pretty well (switched now to other systems). The scrolling I however never fixed. I guess you'd need to add specific data to it and add some custom logic.
@mweibel Your fix worked for me. I handle scrolling with my own code te be able to use a animated scroll. Maybe you should pull request that fix.
@mweibel Great fix thanks, it works for me. Why hasn't this been merged into the main history.js yet?
Worked for me too, nearly three years later!