what is the difference between window.onload and document.ready.
The ready
event occurs after the HTML document has been loaded, while the onload
event occurs later, when all content (e.g. images) also has been loaded.
The onload
event is a standard event in the DOM, while the ready
event is specific to jQuery. The purpose of the ready
event is that it should occur as early as possible after the document has loaded, so that code that adds funcionality to the elements in the page doesn't have to wait for all content to load.
The window onload function is called when the browser hides the loading bar, the document ready function is executed when the DOM (HTML nodes) are ready to edit.
window.onload
is the built-in Javascript event, but as its implementation had subtle quirks across browsers (FF/IE6/IE8/Opera), jQuery provides document.ready, which abstracts those away, and fires as soon as the page's DOM is ready (doesn't wait for images etc.).
document.ready
is a jQuery function, wrapping and providing consistency to the following events:
document.ondomcontentready
/document.ondomcontentloaded
- a newish event which fires when the document's DOM is loaded (which may be some time before the images etc. are loaded); again, slightly different in IE and in rest of the world- and window.onload (which is implemented even in old browsers), which fires when the entire page loads (images, styles, etc.)