window.ready javascript example

Example 1: document ready without jquery

document.addEventListener("DOMContentLoaded", function(event) { 
  //we ready baby
});

Example 2: javascript document load

window.addEventListener("load", function(event) {
    console.log("Tutte le risorse hanno terminato il caricamento!");
});

Example 3: how to trigger events when the document loads in js

document.addEventListener("DOMContentLoaded", ready);
function ready() {
    alert('DOM is ready');

    // image is not yet loaded (unless it was cached), so the size is 0x0
    alert(`Image size: ${img.offsetWidth}x${img.offsetHeight}`);
}

Example 4: js dom ready function

<!doctype html>
<html>
<head>
</head>
<body>
Your HTML here

<script>
// self executing function here
(function() {
   // your page initialization code here
   // the DOM will be available here

})();
</script>
</body>
</html>