Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for using window.onload

I develop Joomla websites/components/modules and plugins and every so often I require the ability to use JavaScript that triggers an event when the page is loaded. Most of the time this is done using the window.onload function.

My question is:

  1. Is this the best way to trigger JavaScript events on the page loading or is there a better/newer way?
  2. If this is the only way to trigger an event on the page loading, what is the best way to make sure that multiple events can be run by different scripts?
like image 551
privateace Avatar asked Feb 17 '09 23:02

privateace


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 is the window onload property generally used for?

Definition and Usage The onload attribute fires when an object has been loaded. onload is most often used within the <body> element to execute a script once a web page has completely loaded all content (including images, script files, CSS files, etc.).

Is window onload deprecated?

Note: The load() method deprecated in jQuery version 1.8. It was completely removed in version 3.0.

Which of the function is the best place to put the code from window onload?

to place that code at the bottom, since you need to use a script (blocking-rendering tag) it's better put it at the end of the document.


1 Answers

window.onload = function(){}; works, but as you might have noticed, it allows you to specify only 1 listener.

I'd say the better/newer way of doing this would be to use a framework, or to just to use a simple implementation of the native addEventListener and attachEvent (for IE) methods, which allows you to remove the listeners for the events as well.

Here's a cross-browser implementation:

// Cross-browser implementation of element.addEventListener() function listen(evnt, elem, func) {     if (elem.addEventListener)  // W3C DOM         elem.addEventListener(evnt,func,false);     else if (elem.attachEvent) { // IE DOM          var r = elem.attachEvent("on"+evnt, func);          return r;     }     else window.alert('I\'m sorry Dave, I\'m afraid I can\'t do that.'); }  // Use: listen("event name", elem, func); 

For the window.onload case use: listen("load", window, function() { });


EDIT I'd like to expand my answer by adding precious information that was pointed by others.

This is about the DOMContentLoaded (Mozilla, Opera and webkit nightlies currently support this) and the onreadystatechange (for IE) events which can be applied to the document object to understand when the document is available to be manipulated (without waiting for all the images/stylesheets etc.. to be loaded).

There are a lot of "hacky" implementations for cross-browsers support of this, so I strongly suggest to use a framework for this feature.

like image 181
Luca Matteis Avatar answered Sep 23 '22 03:09

Luca Matteis