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

JavascriptWeb DevelopmentFront End Technology

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>
raja
Published on 16-Jan-2018 11:33:32
Advertisements