#17283·kit

IFrame highjacking focus from parent window on load

Author: gustavcorpasCreated Oct 1, 2026Updated Oct 1, 2026
Labelstriage-bot

Describe the bug

Problem

Svelte Kit seeks to mimic the browser's focus navigation behaviour.

On loading this can cause the body to get focussed and immediately unfocused.

Current implementation is a document.body.focus() call and later document.body.removeAttribute("tabindex")

This is fine for regular apps / websites but problematic if the app is hosted within an iframe on another page. When the iframe loads, the app does it's focus / de-focus trick, but this leaves the iframe itself as the focussed element on the parent page.

This is an unfortunate side-effect.

Severity

This is making SvelteKit a very undesirable option for pages/apps intended to be hosted within other systems as iframes, such as SaaS platforms with an Apps Marketplace, that often work this way.

Currently one can get around the problem by overriding the focus function, but this is a bad no-no-pattern as well as bad for usability as it breaks the otherwise much desired svelte focus logic.

typescript
const originalFocus = HTMLElement.prototype.focus
HTMLElement.prototype.focus = function(options) {
    if (this === document.body || this === document.documentElement) {
        return
    }
    originalFocus.call(this, options)
}

Possible solution

This is a fix for our specific use case, that seem to keep the intended functionality when navigating within the iframe itself, but skip the focus when loading as part of another page.

When the reset_focus function is called we can include the following check just before the document.body is modified / focussed.

typescript
// ... 
} else focusDocument: {
    // Disable if document is embedded and unfocussed.
        if(window.parent !== window && !document.hasFocus()) {
             break focusDocument;
        }
    const root = document.body;
    // ... continue with focus code

I can create the PR if this is wanted and doesn't come with unintended downsides.

Reproduction

A small online demo is available here and the repo for it is here.

Logs

bash
N/A

System Info

bash
System:
    OS: macOS 15.5
    CPU: (8) arm64 Apple M2
    Memory: 107.22 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 24.5.0 - /opt/homebrew/bin/node
    npm: 11.5.1 - /opt/homebrew/bin/npm
    pnpm: 10.33.0 - /opt/homebrew/bin/pnpm
    bun: 1.2.20 - /opt/homebrew/bin/bun
  Browsers:
    Safari: 18.5

Severity

annoyance

Additional Information

The main aim here is to seek a solution which improves overall parent page accessibility while preserving SvelteKit's focus navigation whenever the iframe is actively focused.

Steps to reproduce are in the online demo and README.md in the repo.