-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Description
In Firefox, setting location.hash = location.hash
will scroll the page back to the current anchor's position. In Chrome and Safari, setting location.hash = location.hash
is ignored.
The HTML Standard's description of the hash
setter steps does not include a special case for checking whether the new hash is equal to the current hash. My reading of the steps is that the browser should 7. Location-object navigate to copyURL
even if copyURL
is the same the current URL. In that case, Firefox is following the spec'd behavior and Chrome and Safari are not.
https://html.spec.whatwg.org/#dom-location-hash
Here is Chromium's hash
setter implementation where it bails early if the new hash is equal to the current hash:
Steps to reproduce:
- Load https://en.wikipedia.org/wiki/Firefox#Features
- Scroll the page.
- In the devtools console, execute
location.hash = location.hash
Result:
In Firefox 94, the page scrolls back to the #Features
anchor position. In Chrome 96 and Safari 15, the page does not scroll.
If you replace step 3 with location.href = location.href
, Firefox, Chrome, and Safari all scroll back to the #Features
anchor position.
This difference in behavior causes a webcompat problem reported in this Firefox bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1733797
Steps to reproduce:
- Load https://lithic.com/legal#terms
- Try to scroll the page using a touchpad, keyboard, or mouse wheel.
Result:
In Chrome 96 and Safari 15, you can scroll the page and the fragment of the URL in the address bar changes as you scroll to each document section on the left of the page ("Terms of Service", "Privacy Policy", etc).
In Firefox 94, you can't scroll the page because the page has a scroll event handler that sets location.hash to the current document section's anchor. Firefox can't scroll because it keeps returning to the current anchor's position.