Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use "window.onload"?

In JavaScript, when I want to run a script once when the page has loaded, should I use window.onload or just write the script?

For example, if I want to have a pop-up, should I write (directly inside the <script> tag):

alert("hello!"); 

Or:

window.onload = function() {     alert("hello!"); } 

Both appear to run just after the page is loaded. What is the the difference?

like image 561
Jonathan Lam Avatar asked Nov 24 '13 20:11

Jonathan Lam


People also ask

When should I use window onload?

window. onload just runs when the browser gets to it. window. addEventListener waits for the window to be loaded before running it.

What does the window onload function do?

The onload property processes load events after the element has finished loading. This is used with the window element to execute a script after the webpage has completely loaded.

What's the difference between document ready () and window onload ()?

ready() is a jQuery event which occurs when the HTML document has been fully loaded, while the window. onload event occurs later, when everything including images on the page loaded.

Should I use onload?

The window. onload event gets fired when all resources - including images, external script, CSS - of a page have been loaded. If you want to do something when that event has been fired you should always use the window.


1 Answers

The other answers all seem out of date

First off, putting scripts at the top and using window.onload is an anti-pattern. It's left over from IE days at best or mis-understandings of JavaScript and the browser at worst.

You can just move your scripts the the bottom of your html

<html>   <head>    <title>My Page</title>   </head>    <body>     content   </body>   <script src="some-external.js"></script>   <script>     some in page code   </script> </html> 

The only reason people used window.onload is because they mistakenly believed scripts needed to go in the head section. Because things are executed in order if your script was in the head section then the body and your content didn't yet exist by definition of execute in order.

The hacky workaround was to use window.onload to wait for the rest of the page to load. Moving your script to the bottom also solved that issue and now there's no need to use window.onload since your body and content will have already been loaded.

The more modern solution is to use the defer tag on your scripts but to use that your scripts need to all be external.

<head>     <script src="some-external.js" defer></script>     <script src="some-other-external.js" defer></script> </head> 

This has the advantage that the browser will start downloading the scripts immediately and it will execute them in the order specified but it will wait to execute them until after the page has loaded, no need for window.onload or the better but still unneeded window.addEventListener('load', ...

like image 123
gman Avatar answered Sep 23 '22 00:09

gman