Uncaught ReferenceError: XXX is not defined means XXX() is not defined.

Uncaught ReferenceError: XXX is not defined, most likely because you put the function in window.onload.

The reasons for this problem are:

1. Window. onload is triggered after the entire page, including the DOM structure, images, etc., has been loaded. The function is there to prevent the HTML from executing the code before it is finished loading, but if you write it in a function you define, the code in the function will execute when you call the function.

2. If you write your own function in window.onload, the scope of that function is window.onload only.

3. The HTML is loaded from top to bottom. When the HTML is loaded, when the function keyword is encountered and a function is declared, a new space will be opened in the memory to store the function, which is convenient to call later.

So, function XXX() inside window.onload()=function(){} is declared after the entire page is loaded, which means that when the HTML is loaded into the calling function, Window. The onload = function () {} in the function of XXX has not been declared, at that time can’t find the function in memory XXX (), and complains.


There are two solutions:

Function XXX() = window.onload()=function(){} function XXX() = window.onload()=function(){}

Var XXX =function(){} var XXX =function(){} The call is made when the scope changes.