jQuery

Difference between document.ready() and body. onload() functions?

Both Body.OnLoad() event and jQuery.ready() event will execute the javascript code when the page is loaded.

 The main differences between the two are:

  • Body.Onload() event will be called only after the DOM and associated resources like images got loaded.jQuery’s document.ready() event will be called once the DOM is loaded i.e., it wont wait for the resources like images to get loaded. Hence, the functions in jQuery’s ready event will get executed once the HTML structure is loaded without waiting for the resources.
  • We can have multiple document.ready() in a page but Body.Onload() event cannot.

Defining Body.Onload() event
<body onload="javascript function name or the atcual script">
Defining jQuery.ready() event
<script type="text/javascript">
$(document).ready(function() {
   //Script goes here
});
</script>
OR
<script type="text/javascript">
        $(function() {
       //Script goes here
        }); 
</script>

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button