Open Bug 840187 Opened 12 years ago Updated 2 years ago

Autofocus overrides anchors

Categories

(Core :: DOM: Navigation, defect)

defect

Tracking

()

People

(Reporter: ah.hamdy, Unassigned)

Details

(Keywords: testcase)

Attachments

(2 files)

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.
Please, attach a minimal testcase (online if you want), so we could test.
Flags: needinfo?(ah.hamdy)
Keywords: testcase-wanted
Dupe for Bug 668213 (which proposed to dupe with bug 633821)?
Component: Untriaged → Document Navigation
Product: Firefox → Core
Flags: needinfo?(ah.hamdy)
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.
Attachment #714072 - Attachment mime type: text/plain → text/html
Attachment #714074 - Attachment mime type: text/plain → text/html
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
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
Status: UNCONFIRMED → NEW
Ever confirmed: true
OS: Windows XP → All
Hardware: x86 → All
Version: 18 Branch → Trunk
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
> 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>.
Severity: normal → S3
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: