Bug 278553

Summary: iframe's document not focusing when .focus() is called
Product: WebKit Reporter: Marcos Caceres <marcosc>
Component: WebCore Misc.Assignee: Marcos Caceres <marcosc>
Status: RESOLVED WORKSFORME    
Severity: Normal CC: ahmad.saleem792, webkit-bug-importer
Priority: P2 Keywords: BrowserCompat, InRadar
Version: Other   
Hardware: Unspecified   
OS: Unspecified   
URL: https://jsfiddle.net/3249g6pw/1/
Bug Depends on:    
Bug Blocks: 268516    

Marcos Caceres
Reported 2024-08-22 22:11:44 PDT
There appears to be an issue with iframes not focusing when .focus() is called on them. Test case: ``` window.onload = async function () { const iframes = document.querySelectorAll("iframe"); for (let iframe of iframes) { await new Promise(resolve => { iframe.onload = resolve; iframe.src = "about:blank"; }); iframe.focus(); console.assert(iframe.contentDocument.hasFocus(), "The iframe is not focused."); } }; ```
Attachments
Radar WebKit Bug Importer
Comment 1 2024-08-29 22:12:11 PDT
Marcos Caceres
Comment 2 2025-02-18 19:18:29 PST
Ahmad Saleem
Comment 3 2026-02-21 10:12:48 PST
(In reply to Marcos Caceres from comment #2) > Test case https://jsfiddle.net/otg682h0/ *** Safari 26.4 Beta *** "iframe1 is not focused after calling focus()" "iframe2 is not focused after calling focus()" "iframe1 is not focused after swapping focus back" "Iframe focus test completed." *** Chrome Canary 147 (147.0.7699.0 (Official Build) canary (arm64) ) *** "Iframe focus test completed." *** Firefox Nightly 149 (20260220092058) *** "Iframe focus test completed."
Marcos Caceres
Comment 4 2026-07-16 06:17:27 PDT
Marcos Caceres
Comment 5 2026-07-19 22:41:43 PDT
After som investigation, what the DC API was doing was actually wrong... so changed the title to fix that instead.
Marcos Caceres
Comment 6 2026-07-21 16:16:27 PDT
*** This bug has been marked as a duplicate of bug 256299 ***
Marcos Caceres
Comment 7 2026-07-21 17:57:31 PDT
Reopening and restoring the original title. Bug 256299 covered the Digital Credentials get() user-attention gating; the underlying issue reported here — iframe.focus() not focusing the iframe's document, where Chrome and Firefox do (comment 3) — is a separate focus bug that remains unfixed. Tracking it here.
Marcos Caceres
Comment 8 2026-07-23 21:51:19 PDT
Testing the reported case shows the failing assertions track whether the top-level document has focus (document.hasFocus()) when focus() is called, and Safari's behavior matches what HTML specifies. HTML's "has focus steps" (https://html.spec.whatwg.org/multipage/interaction.html#has-focus-steps) step 1: "If target's node navigable's top-level traversable does not have system focus, then return false." So document.hasFocus(), and hence iframe.contentDocument.hasFocus(), returns false whenever the containing window lacks system focus, independent of any focus() call. Test: create two iframes, load both about:blank, focus iframe1, iframe2, then iframe1 again, checking contentDocument.hasFocus() after each. - With the window focused, iframe.focus() focuses the iframe's document and every check returns true (Safari, Chrome, and Firefox alike). - With the window not focused, as at window.onload before the window becomes key (where the original test runs), the checks return false. That is the "has focus steps" step 1 result, not a failure of iframe.focus(). Safari is conformant: iframe.focus() focuses the iframe's document, and hasFocus() correctly returns false when the top-level traversable lacks system focus. Closing as WORKSFORME.
Note You need to log in before you can comment on or make changes to this bug.