URL Fragments and pushState() are weeeiiird
Yesterday I learned two weird things that happen when you use pushState() to navigate to a page with a URL fragment:
-
The CSS
:targetselector doesn’t work. You can use:targetto style an element that is the current URL fragment when doing a full document load, but not withpushState! This is also documented on MDN:The target element is set at document load and
history.back(),history.forward(), andhistory.go()method calls. But it is not changed whenhistory.pushState()andhistory.replaceState()methods are called.But that’s weird! Why not? I would argue that it makes
:targeta little bit unusable in modern web applications. Even though it’s such a useful feature! -
The
hashchangeevent doesn’t fire. Even though the hash changes, the event doesn’t fire. This is also documented on MDN, but oddly enough it’s documented in thepushStatedocs, and not in thehashchangedocs.Note that
pushState()never causes ahashchangeevent to be fired, even if the new URL differs from the old URL only in its hash.That’s weird! And unexpected. This note should be included in the
hashchangedocs. Might be a good opportunity to open a small pull request.
Update: I did indeed open a small pull request and the hashchange docs now also explain this behavior.



