I always wonder how the location of the placement of onload event listener will affect the behavior of my Javascript on various browsers. I use the following codes to check out this small doubt of me:
The following are the results of the test on various browser according to the priority of the alert message:
Chrome: "body ends onload", "window.onload in the head", "body begins onload"
Safari/Firefox/IE: "body ends onload", "body begins onload"
I then add an extra onload listener in the body to see if it changes the result. The codes are as follows:
The following are the results of the test on various browser for this new tweak:
Chrome: "body ends onload", "body begins onload", "window.onload in the body"
Safari/Firefox/IE: "body ends onload", "window.onload in the body"
CONCLUSION:
The Chrome browser behave slightly different from the pack. If the window.onload listener is in the head, it will have higher precedence over the onload in the body tag. If the window.onload is in the body, it will have less priority than the one in the body tag with the one in the head ignored.
As for the other browsers, the window.onload in the body will replace the one in the body tag.
One needs to pay special attention to Chrome as it will take all the onload listener in HTML seriously and the programmer needs to make sure the onload script is not running the same script twice or more.
Read More »
<html> <head> <script> window.onload = function() { alert("window.onload in the head"); } </script> </head> <body onload="alert('body begins onload')"> <br><br><br><br><br><br> <script> alert("body ends onload"); </script> </body> </html> |
The following are the results of the test on various browser according to the priority of the alert message:
Chrome: "body ends onload", "window.onload in the head", "body begins onload"
Safari/Firefox/IE: "body ends onload", "body begins onload"
I then add an extra onload listener in the body to see if it changes the result. The codes are as follows:
<html> <head> <script> window.onload = function() { alert("window.onload in the head"); } </script> </head> <body onload="alert('body begins onload')"> <script> window.onload = function() { alert("window.onload in the body"); } </script> <br><br><br><br><br><br> <script> alert("body ends onload"); </script> </body> </html> |
The following are the results of the test on various browser for this new tweak:
Chrome: "body ends onload", "body begins onload", "window.onload in the body"
Safari/Firefox/IE: "body ends onload", "window.onload in the body"
CONCLUSION:
The Chrome browser behave slightly different from the pack. If the window.onload listener is in the head, it will have higher precedence over the onload in the body tag. If the window.onload is in the body, it will have less priority than the one in the body tag with the one in the head ignored.
As for the other browsers, the window.onload in the body will replace the one in the body tag.
One needs to pay special attention to Chrome as it will take all the onload listener in HTML seriously and the programmer needs to make sure the onload script is not running the same script twice or more.