Since Netscape 4 died where we put JavaScript has changed with almost all JavaScript being placed just before the </body> tag which both allows the page content to load faster because it doesn’t have to wait for the scripts to load first and it allows the scripts to run sooner since the page they ineract with is already loaded so the script doesn’t have to wait for anything.
The only two types of scripts I know of that still need to go in the head are:
- framebreaker scripts and similar where the script is deciding whether to continue loading the page or to start a new page load
- scripts such as Modernizr that add classes to the <html> tag so that the way the page displays is adjusted by what JavaScript is supported.
The one type of script I know of that still needs to wait for the load event to trigger before it can run is where the script is going to substitute different images for any externally linked images that fail to load.
Question One: Can anyone think of any other types of script that need to go in the head rather than just before the </body> or which need to wait for the load event to trigger before they run?
I know that any script can go in the head with an appropriate delay applied - but which onces must go there? I also know that all scripts placed at the bottom can apply a delay but which ones will actually fail to work at all if you don’t apply a delay?
Question two: There are lots of advertising scripts that still use antiquated JavaScript to allow them to work in Netscape 4 and earlier. These require the use of postscribe or an equivalent script to be able to place them at the bottom of the page with the rest of the scripts. For example you’d code a Google AdSense ad like this:
<div id="adv1"></div>
...
...
<script type="text/javascript" src="postscribe.js"></script>
<script type="text/javascript" src="htmlParser.js"></script>
<script type="text/javascript">
google_ad_client = "pub-9999999999999999";
google_ad_slot = "9999999999";
google_ad_width = 300;
google_ad_height = 250;
postscribe('#adv1','<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"><\\/script>');
</script>
</body>
Are there any ad services that display ads like Google AdSense that do not use antiquated document.write statements and so can be placed with the rest of the scripts without needing to use postscribe?
I know that there are lots of other types of scripts that might still use document.write but you can always find an alternative to those that doesn’t use that call or alternatively rewrite the script to get rid of that call - something that you cannot do with ad scripts so that using postscribe to dynamically change how document.write works is the only alternative to actually jumbling the scripts through the page.