Skip to content

window onload does not fire up in chromeΒ #14873

@zilberlex

Description

@zilberlex

Describe the bug

When i use <svelte:window onload={onLoad}> it does not work in chrome (And also not in svelte.dev environments), but it does work in Firefox.

After talking with chatgpt the fix was:


onMount(() => {
    if (document.readyState === 'complete') {
        onLoad();
    } else {
        // Otherwise, wait for the load event
        window.addEventListener('load', onLoad, { once: true });
    }
});

function onLoad(_event) {
    console.debug('loaded window.');
    isAppLoaded = true;
}


According to chatgpt onload event may fire before the sveltecode which attaches the eventhandler executes.

I am not a big expert in WebDev, but if I understand this correctly, it is not only a svelt issue. It would be nice if the had already the fix above implmeneted, if that is even the correct way to handle that.

Reproduction

https://svelte.dev/playground/0f493cd43f36479fb6c907fa08d16ed3?version=5.16.0

Currently, it is successfully reproduces in the online environemnt of svelte.dev, but due to the nature of the issue (race condition), there might be a chance it won't reproduce in the future.

Logs

No response

System Info

Independant Scripts:
  Chromium (131.0.2903.99)    

Internet Explorer: 11.0.26100.1882
  npmPackages:
    svelte: ^5.0.0 => 5.16.0

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions