Closed Bug 505355 Opened 16 years ago Closed 6 years ago

input type=file cannot accept focus using javascript

Categories

(Core :: Layout: Form Controls, defect)

x86
Windows XP
defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla68
Tracking Status
firefox68 --- fixed

People

(Reporter: gcgabfest, Assigned: emilio)

References

Details

(Keywords: regression)

Attachments

(3 files)

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1 (.NET CLR 3.5.30729) Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1 (.NET CLR 3.5.30729) If you put the html below in a page you can press the "focus file" button to focus on the inputFile. This works as expected in IE8 (IE7 compatibility mode off), Chrome 2.0, Safari 4.0, you can at least focus on the browse button. However the test fails in FF 3.0 and FF 3.5, the focus is just lost. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <body> <form> <script> function focusInput() { var input = document.getElementById("dmoTpl:testid::content1"); input.focus(); } function focusFile() { var input = document.getElementById("dmoTpl:testid::content2"); input.focus(); } </script> <button onclick="focusInput();return false;">focus input</button> <br> <input id="dmoTpl:testid::content1"> <br> <button onclick="focusFile();return false;">focus file</button> <br> <input type="file" size="10" id="dmoTpl:testid::content2"/> </form> </body> </html> Reproducible: Always Steps to Reproduce: run the html given above in FF3.0 or FF3.5, hit the 'focus file' button. Actual Results: when you hit the 'focus file' button note the focus doesn't move to the inputFile, not even to the button Expected Results: when you hit the 'focus file' button expect it to move to the browse button
Component: General → Layout: Form Controls
Product: Firefox → Core
QA Contact: general → layout.form-controls
Version: unspecified → Trunk
Seems like a likely regression from bug 299677? Not sure why that would have this effect. Neil, Martijn, can you have a look?
Status: UNCONFIRMED → NEW
Ever confirmed: true
Regarding why I need this, I work on ADF Faces, a set of JSF components. We have an inputFile component that brings up an input typ=file in an inline popup and when the popup is opened we set focus on the file input. You can see a demo here: http://jdevadf.oracle.com/adf-richclient-demo/faces/components/inputFile.jspx hit the 'Update...' button next to 'oracle.gif', note that when the popup is opened in FF3 the focus is back to the start of the page instead of on the file input because the attempt to set focus on the file input has failed.
I could narrow it down further and bug 299677 is not in that range anymore. New range, including an hour more (2005082412 was the first build with the bug): http://bonsai.mozilla.org/cvsquery.cgi?module=PhoenixTinderbox&date=explicit&mindate=2005-08-24+07%3A00&maxdate=2005-08-24+13%3A00
Oh, probably bug 303620 then.
Blocks: 303620
Blocks: 1004356
Attaching testcase showing that file fields can't be focused with JavaScript. Another use case is for focusing the button of a file input on pages loaded with AJAX, similar to how `autofocus` works for regularly loaded pages. To workaround the security issue of users being tricked into selecting a file (eg, by holding "Enter" and having the file field selected), the submit button for the upload dialog can have a delay of a few seconds (like the download dialog does).
Attaching example of TEXT input focus working. With both attachments, simply open it, and the field should be focused.
Another use-case is tabfocus plugin of TinyMCE editor which provides TAB support for going in/out of the editor (which is an iframe).

I did a bit on testing on this bug, and I think this might have to more to do with trying to focus the element before loading is finished.

window.onload = () => {
    document.querySelector("input[type=file]").focus();
}

This code can actually focuses the button inside the <input type=file>.

When running the code from comment 7 the GetPrimaryFrame call inside HTMLInputElement::Focus returns null. (source)

Told Tom I'll take a quick look at this while I look to bug 1495621 too, so ni? so I don't forget :)

Flags: needinfo?(emilio)
Depends on: 1543161

This is closer to what other UAs do, it's simpler, and fixes the bug.

It looks like the complexity of multiple buttons or what not is related to
bug 1188880, which is WONTFIX. We no longer have multiple buttons in the same
file input, so this is better IMO.

Assignee: nobody → emilio
Flags: needinfo?(emilio)
Pushed by emilio@crisal.io: https://hg.mozilla.org/integration/mozilla-inbound/rev/d538a2181589 Simplify focus handling for file inputs. r=smaug
Status: NEW → RESOLVED
Closed: 6 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla68
Created web-platform-tests PR https://github.com/web-platform-tests/wpt/pull/16501 for changes under testing/web-platform/tests
Regressions: 1582323

Bugbug thinks this bug is a regression, but please revert this change in case of error.

Keywords: regression
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: