Does document.ready() function similar to onload()? (or) Explain the importance of document.ready()? (or) Why jQuery script is faster?Explain


Document.ready() function is completely different from body onload() function .
Explanation
JavaScript Operations on the page elements are performed outside of the DOcument Markup (DOM) that creates them. This is because of waiting until  DOM elements of the page are fully loaded before those operations execute. The onload handler for the window object is used for this purpose, executing statements after the entire page is fully loaded.
Browser delays calling onload till DOM tree is created and then  all images/other external resources are fully loaded.
jQuery waits until DOM tree is ready before executing scripts. jQuery uses this approach using Document.Ready . This makes running scripts fast. jQuery triggers execution of code once the DOM tree is loaded , but not external image resources has loaded.  Hence script execution need not wait till the page is loaded. The code execution happens before page is loaded hence jQuery code is faster. We can use this technique multiple times within the same HTML document.
To summarize the differences
First of all Document.ready() function is called as soon as DOM is loaded but body.onload() function is called when everything gets loaded on the page that includes DOM, images and all associated resources of the page.
We can have as many document.ready() functions in a page but onload function is limited on one time.

    0 comments:

    Post a Comment