0

How to implement $(document).ready functionality without using jQuery?

Simply use the DOMContentLoaded event which fires when the DOM is loaded, but before all page assets are loaded (styles, images, etc.).

document.addEventListener("DOMContentLoaded", function(event) { 
  // do something
});
<!doctype html>
<html>
	<head>
      <title>DOMContentLoaded Example</title>
  	  <script>
    	var DOMContentLoaded = function(a,b,c){b=document,c='addEventListener';b[c]?b[c] ('DOMContentLoaded',a):window.attachEvent('onload',a)}
    
      DOMContentLoaded(function () {
        console.log('The DOM is Ready!');
      });
  </script>
  </head>
  <body>
  	<h1>DOMContentLoaded Example</h1>
  	<p>Super small Cross-browser DOM ready function</p>
  </body>
</html>
RUN
add comment
Suggest a different solution or add another example