javascriptdombrowsersveltesvelte-5

Svelte:window component onload not triggering


In Svelte5, why is the onload event not triggering the function foo() in src/routers/+layout.svelte?

<script>
    const foo = () => console.log('foo triggered')
</script>

<svelte:window onload={foo} />

Solution

  • I'm guessing here, but most likely the load event on the document has already fired before your own JS code runs that adds the listener for that event. In a +layout.svelte, you should use onMount() instead.