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!");


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

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

Updated on: 13-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started