We are using window.onload or body onload=”” to start performing some events as the page is loaded.
Like
[cc lang=”javascript”]window.onload=myFunction;[/cc]
There are some down side to this method. First of all we have to wait for images. Next, what if you had a few scripts and had some code tied to window.onload? They would overwrite each other.
Mootools have created a solution for this. Mootools comes with a custom event that fire as soon as the DOM is ready. It is called DomReady.
[cc lang=”javascript”]
Window.addEvent(‘domready’, function(){
myFunction();
});
[/cc]
Above code will fire the myFunction as soon as all of the HTML DOM is loaded but before images, other windows and other images are loaded at all.
Benefits of DomReady Event:
- Allow you to work with page as sooner than onload.
- Allow you to use the same event with many scripts.
- No waiting for images.
DomReady Syntax
[cc lang=”javascript”]
/*
MooTools DomReady.
Two Styles.
1. Call the function directly.
2. The function is inside of anonomos function.
*/
window.addEvent(‘domready’, myFunction);
//Or.
window.addEvent(‘domready’, function(){
myFunction();
});
function myFunction()
{
alert(“The DOM is ready.”);
}
[/cc]
[…] This post was mentioned on Twitter by Avinash Zala, Avi. Avi said: Mootools domready vs window.onload: Mootools have created a solution for this. Mootools comes with a custom event … http://bit.ly/aTYreZ […]
[…] console.log('DOM is ready….') });In earlier post I have explained the difference between domready and window.load.New Element CreationFor making the rich web application and/or working with AJAX you might have to […]
[…] This trick will be useful when you want to check if the element with specific ID is available in DOM or not. This tricks is preferable to use on domready event of the page. If you are not sure what does domready means then I suggest you to have a look at this article related to domready. […]
[…] than the window.load event, which waits for all other resources to load. I suggest reading this in depth comparison of DomReady and window.load. The following example uses DomReady to wait until the DOM loads before querying […]