What is $(document).ready() equivalent in JavaScript?


In jQuery, if you want an event to work on your page, you should call it inside the $(document).ready() function. Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded.

$(document).ready(function() {
   alert(“Document loaded successful!");
});

Example

In JavaScript, to achieve the same result like $(document).ready, try the following code −

<html>
   <body>
      <script>
         var loader = setInterval(function () {
            if(document.readyState !== "complete") return;
            clearInterval(loader);
            alert(“Document loaded successful!");
            // document.write("Document loaded successful!");
         }, 300);
      </script>
   </body>
</html>

Updated on: 13-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements