Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What should happen when setting location.hash = location.hash? #7386

Closed
cpeterso opened this issue Dec 4, 2021 · 4 comments · Fixed by #7473
Closed

What should happen when setting location.hash = location.hash? #7386

cpeterso opened this issue Dec 4, 2021 · 4 comments · Fixed by #7473
Labels
interop Implementations are not interoperable with each other topic: navigation

Comments

@cpeterso
Copy link

cpeterso commented Dec 4, 2021

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:

https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/core/frame/location.cc;l=203-204;drc=984c3018ecb2ff818e900fdb7c743fc00caf7efe

Steps to reproduce:

  1. Load https://en.wikipedia.org/wiki/Firefox#Features
  2. Scroll the page.
  3. 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:

  1. Load https://lithic.com/legal#terms
  2. 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.

@Yay295
Copy link
Contributor

Yay295 commented Dec 4, 2021

Setting location.href to location.href reloads the page, so Firefox's behavior for location.hash makes sense to me.

@domenic
Copy link
Member

domenic commented Dec 4, 2021

We can update the spec to match 2/3 browsers; I'll try to work on that soon.

@domenic domenic added interop Implementations are not interoperable with each other topic: navigation labels Dec 4, 2021
@cpeterso
Copy link
Author

cpeterso commented Dec 5, 2021

Thanks. In that case, I'll submit a patch to make Firefox's behavior match Chrome and Safari's. I also have a patch to extend the scroll-to-anchor-name.html WPT test to cover this location.href = location.href corner case.

@annevk
Copy link
Member

annevk commented Dec 6, 2021

Note that the new value post-parsing is what is being compared to the old value. This makes sense, but also means it's slightly more involved. It might be worth testing that as well with a value such as .

domenic added a commit to web-platform-tests/wpt that referenced this issue Jan 7, 2022
domenic added a commit that referenced this issue Jan 7, 2022
This adds a special case which is necessary for compatibility with deployed content, and implemented in 2/3 engines. Closes #7386.
domenic added a commit that referenced this issue Jan 12, 2022
This adds a special case which is necessary for compatibility with deployed content, and implemented in 2/3 engines. Closes #7386.
domenic added a commit to web-platform-tests/wpt that referenced this issue Jan 12, 2022
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this issue Feb 5, 2022
…he current hash, a=testonly

Automatic update from web-platform-tests
Test using location API to navigate to the current hash

See whatwg/html#7386.

--

wpt-commits: 1e5972db2fb855ac1a1685b95b11a5f7159e4c4c
wpt-pr: 32291
jamienicol pushed a commit to jamienicol/gecko that referenced this issue Feb 7, 2022
…he current hash, a=testonly

Automatic update from web-platform-tests
Test using location API to navigate to the current hash

See whatwg/html#7386.

--

wpt-commits: 1e5972db2fb855ac1a1685b95b11a5f7159e4c4c
wpt-pr: 32291
mfreed7 pushed a commit to mfreed7/html that referenced this issue Jun 3, 2022
This adds a special case which is necessary for compatibility with deployed content, and implemented in 2/3 engines. Closes whatwg#7386.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interop Implementations are not interoperable with each other topic: navigation
Development

Successfully merging a pull request may close this issue.

4 participants