SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,799
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)

    Script Placement and Types of Script

    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:
    1. framebreaker scripts and similar where the script is deciding whether to continue loading the page or to start a new page load
    2. 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:

    Code:
    <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.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,799
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    As it has been over three days since I posted the above questions and no one has commented, does that mean that no one knows of any other scripts that need to go in the head or need to wait for the load event other than the three that I mentioned and that no one knows of any ad script written for relatively modern browsers such as Internet Explorer Five rather than for antiquated ones such as Netscape four?
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    The only ones that come to mind are advertising tagging like you already described. In particular I know the Yahoo APT tagging must be located in the head without using something like postscribe. There is probably some analytics tagging out there somewhere that uses document.write as well. Though none that I'm aware of. I think it is safe to say with services like Tealium most vendor tagging uses proper methods. Before leaving my last job there was an initiative in place to move all our tagging over to Tealium and from what I know the only one that could not be was Yahoo APT ad tagging. Pretty open ended question considering the number of vendors out there that provide web site tagging. There will always be exceptions to the rules.
    The only code I hate more than my own is everyone else's.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •