Bug 262542 - history.back() does not restore scroll position
Summary: history.back() does not restore scroll position
Status: RESOLVED INVALID
Alias: None
Product: WebKit
Classification: Unclassified
Component: History (show other bugs)
Version: Safari 16
Hardware: Mac (Intel) macOS 13
: P2 Normal
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2023-10-03 08:04 PDT by Christian Taylor
Modified: 2023-10-03 11:39 PDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Christian Taylor 2023-10-03 08:04:56 PDT
When navigating back to a long page using `history.back()` or `history.go(-1)` the browser returns to the page and resets the scroll position to `0`.
However, performing the same navigation using the browser's "back" button restores the scroll position on the previous page.
I would expect `history.back()` to behave the same as clicking the back button and restore the previous page's scroll position.
Comment 1 Simon Fraser (smfr) 2023-10-03 10:29:25 PDT
Do you have an example of a page that shows this bug?
Comment 2 Radar WebKit Bug Importer 2023-10-03 10:29:37 PDT
<rdar://problem/116408614>
Comment 3 Christian Taylor 2023-10-03 11:25:55 PDT
Ah, I put together a demo and found an explanation.

My link markup looked like this:

`<a href="/previous" onclick="history.back()">Back</a>`

I realized `back()` is asynchronous so the `href` is being followed as though the link were clicked.

If I change the `onclick` to `event.preventDefault();history.back()` things work as I would expect.

I was thrown off because `onclick="history.back()"` works in FireFox without needing the `preventDefault()`.

I think this report can be closed. Thanks for letting me rubber duck this issue.
Comment 4 Simon Fraser (smfr) 2023-10-03 11:39:35 PDT
Thank you for checking.