As far as I can tell, it is in part determined by the HTML spec's History API1, specifically the value of history.scrollRestoration
. Quoting the HTML spec's scroll restoration mode paragraph:
"auto"
The user agent is responsible for restoring the scroll position upon navigation.
"manual"
The page is responsible for restoring the scroll position and the user agent does not attempt to do so automatically.
This answer2 illustrates how the "manual"
option can be used, but what about "auto"
? Is the behavior then set by user agent implementations? If this is true, is my assumption correct that one would have to take the "manual"
route to ensure that the back button's behavior is deterministic across browsers? (If they properly implemented the History API1, that is.) The example in the "Context" section below seems to confirm this.
[1]: HTML spec
: 7.2 APIs related to navigation and session history > 7.2.5 The History interface; 7.4 Navigation and session history
MDN
: History API
[2]: The threads I have found in this topic (1, 2, 3) show activity from around 2014 and / or provide an answer without much explanation or reference to sources.
Context
We are trying to troubleshoot a page of legacy web application that is mostly used by screen reader users (via JAWS), but the behavior described below are the same for all users regardless:
Click on a client link in a long
<table>
of rows along the lines of:Assignment Date Client Notes Instructor Assigned By Status April 12, 2024 Doe, John blabla Goodworker, Sally Bigboss, Paige Assigned On the client's page, the user clicks on a button; such as:
<input type="button" aria-label="Plans" onclick="location.href='/plans/1234';" value="Plans" >
Click on the browser's back button.
Focus:
JAWS
: the "Plan" button in step 2.Chrome
3:<body>
, but when hitting TAB, the focus jumps to the very first link, and not to the button clicked previously.Firefox
4,5: the "Plan" button in step 2.
Click on the browser's back button.
Focus:
JAWS
:<body>
; on TAB, focus jumps back to link used to navigate away.Chrome
3:<body>
; on TAB, focus jumps to link in the next line.Firefox
4,5: the link used to navigate away from the page in step 1.
[3]: Chrome DevTools: Track element focus
[4]: Determined by issuing document.activeElement
in the dev console after each back button press.
[5]: For the record, Firefox does retain scroll information in this page in our web app, but when I tested this MDN page, the results were inconsistent. For example, search for "single-page application", the first result is a link, clicking on it, then going back shows the anchor as focused. There is a link right below saying fetch()
, but clicking and going back will show that the focus is on <body>
...
update:
chrome
: Wanted to ask this on the chromium-discuss Google Group, but none of my messages went through, so opened an issue (which has been confirmed to be a bug).firefox
: Asked about this in the Firefox chat as well.
<a>
element seems like it would be more appropriate and might produce more consistent back button results across user agents. – Scrouge