+ 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 ? ---- â
6 Answers
+ 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.
+ 4
Ginfio
// This runs when the page loads
window.onload = () => {
if (condition) {
doSomething();
}
}
+ 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.
+ 3
Daniel C can if statement be inside it?
+ 3
Daniel C one more q.
Can we just put it through the whole javascript?
window.onload = () =>{
All J.S
}