Usually we need to load the script when the page is opened, which must be executed after the page has been loaded, because the DOM is complete and can be used by window.onload to ensure this, such as:
Window.onload=firstfunction;
This script means to execute the Firstfunction function after the page is finished, but when there are many functions that need to be executed when the page is loaded? Some may say that this is possible:
Window.onload=firstfunction;
Window.onload=secondfunction;
However, this will only execute the Secondfunction function.
Simon Willison Blog offers the perfect solution:
[JavaScript]View PlainCopy
- function Addloadevent (func) {
- var oldonload = window.onload;
- if (typeof window.onload! = ' function ') {
- Window.onload = func;
- } Else {
- Window.onload = function () {
- if (oldonload) {
- Oldonload ();
- }
- Func ();
- }
- }
- }
The func parameter is the name of the function to be loaded (remember only the function name, not the parentheses), how many functions need to be executed after the page load, and the number of times the above classic function is called, for example:
Addloadevent (firstfunction);
Addloadevent (secondfunction);
......
Execute multiple JS functions after page loading