javascript load after page load code example

Example 1: JavaScript that executes after page load

//two ways of executing JS code after page is loaded, use "DOMContentLoaded" when able

document.addEventListener("DOMContentLoaded", function(){
    //dom is fully loaded, but maybe waiting on images & css files
});

window.addEventListener("load", function(){
    //everything is fully loaded, don't use me if you can use DOMContentLoaded
});

Example 2: javascript document load

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

Example 3: window.onload execute after load page

/* javascript function is executed after 5 seconds page was loaded */
window.onload = function() {
   setTimeout(loadAfterTime, 5000)
}; 
 
function loadAfterTime(){
   document.getElementById("menu").style.display="block";
}

Example 4: call javascript function after page load complete

//for jquery
//after document load
$(document).ready(function() {   //same as: $(function() { 
     alert("hi 1");
});
//after full window load including image src css file
$(window).load(function() {
     alert("hi 2");
});

Example 5: wait for page load js

window.addEventListener('load', function () {
  
})

//THE OTHER ANSWER IS WRONG (has a syntax error)

Example 6: html tag run only after whole page is loaded

<body onload="script();">
<!-- will call the function script when the body is load -->