+ 6

When and how to use window.onload = () =>{ }

When do we use window.onload = () =>{ }, Where should the end (}) curly brace end? When should I NOT use it? ----------- Any additional Tips / suggestions ? ---- ❓

29th Jun 2019, 7:29 PM
Ginfio
Ginfio - avatar
6 ответов
+ 4
window.onload is called once the tab is loaded by the browser. It can also be rewritten as window.onload = function() {} Inside the curly braces you simply put the code you want to run once the page is loaded. On sololearn, you can't access the document (ex. document.getElementById) until the page is loaded, so it makes sense to use it in there.
29th Jun 2019, 7:56 PM
Daniel C
Daniel C - avatar
+ 4
Ginfio // This runs when the page loads window.onload = () => { if (condition) { doSomething(); } }
29th Jun 2019, 7:58 PM
Daniel C
Daniel C - avatar
+ 4
Ginfio You technically could, but in general you shouldn't. From what I know it's typically just used for setting up a script and giving values to variables.
29th Jun 2019, 8:06 PM
Daniel C
Daniel C - avatar
+ 3
Daniel C can if statement be inside it?
29th Jun 2019, 7:57 PM
Ginfio
Ginfio - avatar
+ 3
Daniel C one more q. Can we just put it through the whole javascript? window.onload = () =>{ All J.S }
29th Jun 2019, 8:02 PM
Ginfio
Ginfio - avatar