Autofocus overrides anchors

NEW
Unassigned

Status

()

Core
Document Navigation
5 years ago
3 years ago

People

(Reporter: Ahmed, Unassigned)

Tracking

({testcase})

Trunk
testcase
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

Attachments

(2 attachments)

(Reporter)

Description

5 years ago
User Agent: Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0
Build ID: 20130104151925

Steps to reproduce:

Having an input box with autofocus in the beginning of the pages, and then having an anchor(s) later in the page. 


Actual results:

Linking to an anchor inside the page from an external page would fail to jump to the anchor. Clicking again after the page loads will jump to the anchor.


Expected results:

The browser should jump to the anchor even if the autofocus exists. IE and Chrome does the right behavior.

Removing the autofocus gets firefox to jump to the anchor directly.

Comment 1

5 years ago
Please, attach a minimal testcase (online if you want), so we could test.
Flags: needinfo?(ah.hamdy)

Updated

5 years ago
Keywords: testcase-wanted

Comment 2

5 years ago
Dupe for Bug 668213 (which proposed to dupe with bug 633821)?
Component: Untriaged → Document Navigation
Product: Firefox → Core
(Reporter)

Comment 3

5 years ago
Created attachment 714072 [details]
Test case where autofocus for the input box will override the anchors.
Flags: needinfo?(ah.hamdy)
(Reporter)

Comment 4

5 years ago
Created attachment 714074 [details]
Test case where autofocus was removed from the input box. Now anchors work.

Try to reach an example anchor #Q using this file and using the other file with autofocus. The first time to reach this anchor will fail if there is an autofocus, if you try AGAIN while the page is LOADED. The browser will jump.

Updated

5 years ago
Attachment #714072 - Attachment mime type: text/plain → text/html

Updated

5 years ago
Attachment #714074 - Attachment mime type: text/plain → text/html

Comment 5

5 years ago
Works for me with 
2013-02-14-03-28-50-mozilla-central-firefox-21.0a1.en-US.linux-x86_64
firefox-18.0.1.ru.linux64
Keywords: testcase-wanted → testcase

Comment 6

3 years ago
I can confirm the behavior in Firefox 34.0 (Mac OS X 10.8.5) and Firefox Nightly 37.0a1 (2014-12-20). My test is available at http://medialize.github.io/ally.js/tests/browser-bugs/blink-autofocus.html#foo

I can also add that Blink (not WebKit) has the same issue: https://code.google.com/p/chromium/issues/detail?id=382901

Updated

3 years ago
Status: UNCONFIRMED → NEW
Ever confirmed: true
OS: Windows XP → All
Hardware: x86 → All
Version: 18 Branch → Trunk

Comment 7

3 years ago
When both, autofocus and fragment-anchor are present, the following event sequence is played:

init (synchronous script at end of document)
  target: null
  activeElement: BODY

focus event
  event.target: #document

focus event
  event.target: INPUT

readystatechange event
  readyState: interactive
  target: null
  activeElement: INPUT

DOMContentLoaded
  target: DIV
  activeElement: INPUT

readystatechange event
  radyState: complete
  target: DIV
  activeElement: INPUT

load event
  target: DIV
  activeElement: INPUT

The fragment's target (`:target`) is only accessible after DOMContentLoaded, the autofocus element receives focus *before* the document enters interactive state.

Disabling the autofocus by adding the following script to the end of the document has will cause the <input> to not have that attribute anymore, but is still focused right away:

<script>document.querySelector('[autofocus]').removeAttribute('autofocus');</script>

There is no way to *prevent* the <input> from receiving focus, you can only blur it, and move the document back to the :target (using Element.scrollIntoView [1]) after the fact.

[1] https://developer.mozilla.org/en-US/docs/Web/API/Element.scrollIntoView

Comment 8

3 years ago
> Disabling the autofocus by adding the following script to the end of the document has will
> cause the <input> to not have that attribute anymore, but is still focused right away:
> <script>document.querySelector('[autofocus]').removeAttribute('autofocus');</script>

While that holds true, removing the [autofocus] element will, of course, issue a blur event, making <body> the activeElement and fragment target remains in the viewport.

After pressing <kbd>Command R</kbd> to reload the document, scroll position moves to the beginning, as if the [autofocus] element was moved into viewport. This does not happen with <kbd>Command Shift R</kbd>.
You need to log in before you can comment on or make changes to this bug.