Web Development Ninjas Question:
Download Job Interview Questions and Answers PDF
Explain me difference between JavaScript window.onload event and jQuery ready function?
Answer:
This is the follow-up of the previous question. The main difference between the JavaScript onload event and the jQuery ready function is that the former not only waits for DOM to be created but also waits until all external resources are fully loaded including heavy images, audios and videos. If loading images and media content takes lot of time, then the user might experience significant delay on the execution of code defined in the window.onload event.
On the other hand, the jQuery ready() function only waits for the DOM tree, and does not wait for images or external resource loading, something that means faster execution. Another advantage of using the jQuery $(document).ready() is that you can use it at multiple times in your page, and the browser will execute them in the order they appear in the HTML page, as opposed to the onload technique, which can only be used for a single function. Given this benefits, it’s always better to use the jQuery ready() function rather than the JavaScript window.onload event.
On the other hand, the jQuery ready() function only waits for the DOM tree, and does not wait for images or external resource loading, something that means faster execution. Another advantage of using the jQuery $(document).ready() is that you can use it at multiple times in your page, and the browser will execute them in the order they appear in the HTML page, as opposed to the onload technique, which can only be used for a single function. Given this benefits, it’s always better to use the jQuery ready() function rather than the JavaScript window.onload event.
Download Ninjas Web Developer Interview Questions And Answers
PDF
Previous Question | Next Question |
Suppose you have five <div> element in your page? How do you select them using jQuery? | Tell me how do you add and remove CSS classes to an element using jQuery? |