Home >Web Front-end >JS Tutorial >JavaScript method to execute a specified function after the web page is loaded_javascript skills

JavaScript method to execute a specified function after the web page is loaded_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:08:051272browse

The example in this article describes how JavaScript implements the execution of the specified function after the web page is loaded. Share it with everyone for your reference. The specific analysis is as follows:

The following JS code demonstrates how to call the specified function when the web page is loaded, and multiple functions can be dynamically added to execute simultaneously through the second piece of code.

We only need to specify a 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 at the same time after the page is loaded, you can achieve it through the following JS code

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 will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn