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
:target
selector doesn’t work. You can use:target
to 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
:target
a little bit unusable in modern web applications. Even though it’s such a useful feature! -
The
hashchange
event 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 thepushState
docs, and not in thehashchange
docs.Note that
pushState()
never causes ahashchange
event 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
hashchange
docs. 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.