After testing the two pieces of code separately, we found that the window.onload without parentheses showed the correct effect after the page was loaded. I think window.onload is also equivalent to a trigger event, if the function has parentheses, then directly execute the function, then load the body. Without parentheses, it is equivalent to assigning a function as a variable to the window.onload and not loading immediately.
Code One:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <script> window.onload = doIt (); function doIt () {var odiv = document.getElementById ("Div1"); alert (ODIV); } </script> </pead> <body> <div id= "Div1" ></div> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Code two:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <script> window.onload = doIt; function doIt () {var odiv = document.getElementById ("Div1"); alert (ODIV); } </script> </pead> <body> <div id= "Div1" ></div> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]