JavaScript implements the method to execute the specified function when the web page is loaded

  • 2020-05-17 04:50:23
  • OfStack

This article illustrates how JavaScript implements the specified function when a web page is loaded. Share with you for your reference. The specific analysis is as follows:

The JS code below demonstrates how to call the specified function when the page is loaded, and you can dynamically add multiple functions to execute at the same time through the second section of the code.

We only need to specify one function for window.onload to automatically execute the MyCoolInitFunc function when the page is loaded


<script type="text/javascript" >
  window.onload = MyCoolInitFunc
</script>

If you want to execute multiple functions in parallel after the page is loaded, you can do this with the JS code below


function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  }
  else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

I hope this article is helpful for you to design javascript program.


Related articles: