javascript page ready code example

Example 1: dom ready js

document.addEventListener("DOMContentLoaded", function() {
  // code
});

Example 2: js on page ready

document.addEventListener("DOMContentLoaded", function(event){
  // your code here
});

Better than 

window.onload = function(){
    // code goes here
};

Example 3: 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>