Answers for "execute js after page load"

17

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
});
Posted by: Guest on July-31-2019
2

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";
}
Posted by: Guest on October-30-2020
1

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");
});
Posted by: Guest on May-29-2020
0

javascript execute code on page load

<html>
 
  <body onload="loaded();"></body>
  <script>
    function loaded() {
      alert('Page is loaded');
    }
 
  </script>
 
</html>
Posted by: Guest on November-18-2020
1

html tag run only after whole page is loaded

<body onload="script();">
<!-- will call the function script when the body is load -->
Posted by: Guest on September-11-2020

Code answers related to "execute js after page load"

Code answers related to "Javascript"

Browse Popular Code Answers by Language