Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
|
The body tag is not required to create an onload event, in fact, it is considered bad form by some developers to ever place it directly onto that opening tag. Onload means that the browser will attempt to wait for all images, and all HTML to load before running the script.
If you run the above script at an arbitrary location in the it will run in the order that the document is created. This can have the side effect of slowing things down a bit, since JavaScript is a single threaded language and must execute before deferring to other actions, such as loading the rest of the HTML. Some developers, to avoid this, will place their scripts at the end of the page, right before the closing </body> tag.
You may also defer a script by placing it inside of an onload event without placing it inline onto the opening <body>:
HTML Code:
<script type="text/javascript">
window.onload = MM_preloadImages('_images/common/add-to-cart_hover.gif','_images/common/preorder_hover.gif');
</script>
Note, however, that more than one onload event is not allowed per page; so if you already have one you shouldn't build another one.
|