, the common solution is as follows:
| The code is as follows |
Copy Code |
<script type= "Text/javascript" > var func = window.onload; Window.onload = function () { Func? Func (): 0; Alert (1); } </script> |
The principle is to pass the Window.onload event one level at a time to ensure that every window.onload event can be executed, but if there are multiple calls and variable names, it will produce too much recursion error, as follows
| The code is as follows |
Copy Code |
<script type= "Text/javascript" > var func = window.onload; Window.onload = function () { Func? Func (): 0; Alert (1); }
func = window.onload; Window.onload = function () { Func? Func (): 0; Alert (2); } </script> |
The above implementation executes two window.onload events, but because each pass window.onload event is defined as the same variable name, a too much recursion error is generated.
Solving method
The reason for this is that the definition of the same variable name produces loop execution code, so the solution needs to be resolved only by defining the variable name differently. As follows
| The code is as follows |
Copy Code |
<script type= "Text/javascript" > var func = window.onload; Window.onload = function () { Func? Func (): 0; Alert (1); }
func1 = window.onload; Window.onload = function () { Func1? Func1 (): 0; Alert (2); } </script> |
If a Web page appears multiple Window.onload events, try to merge operations processing, or implement a jquery-like ready event mechanism to solve redundant code problems and improve the readability of your code.