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:
- Is this the best way to trigger JavaScript events on the page loading or is there a better/newer way?
- 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?
As of I always include jQuery/bootstrap JS files on bottom of document and have no acces to
$
over the document, I developed a tiny "init" plugin which is one and only JS script I include on very top of my pages:Using this I can define any anonymous loader over the page, before jQuery and bootstrap inclusion and stay sure that it will fire once jQuery is present:
Personally, I prefer this method. Not only does it allow you to have multiple
onload
functions, but if some script had defined it before you got to it, this method is nice enough to handle that... The only problem left is if a page loads several script which does not use thewindow.addLoad()
function; but that is their problem :).P.S. Its also great if you want to "chain" more functions later on.
Modern javascript frameworks have introduced the idea of a "document ready" event. This is an event that will fire when the document is ready to have DOM manipulations performed on it. The "onload" event fires only after EVERYTHING on the page has loaded.
Along with the "document ready" event, the frameworks have provided a way to queue up multiple bits of Javascript code and functions to run when the event fires.
So, if you're opposed to frameworks, the best way to go about this is to implement your own document.onload queue.
If you're not opposed to frameworks, then you'll want to look into jQuery and document.ready, Prototype and dom:loaded, Dojo and addOnLoad or Google for [your framework] and "document ready",.
If you haven't picked a framework but are interested, jQuery is a good place to start. It doesn't change any of Javascript's core functionality, and will generally stay out of your way and let you do things as you like when you want to.
Joomla ships with MooTools, so you'll find it easiest to use the MooTools library for your additional code. MooTools ships with a custom event called domready that fires when the page is loaded and the document tree is parsed.
More information about MooTools can be found here. Joomla 1.5 currently ships with MT1.1 while the Joomla 1.6 alpha will include MT1.2
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
andattachEvent
(for IE) methods, which allows you to remove the listeners for the events as well.Here's a cross-browser implementation:
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 theonreadystatechange
(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.
This is the dirty but shorter way :P