Does it matter where in a XHTML document I place the following code; Does it HAVE to be in the <head> or can it be in the <body> and does it have to be at the beginning of those nodes or can it be anywhere within them? What effects will it have by being in different places? Thanks, Michael
It depends on the purpose of the script. The majority of scripts go in the header or rather the best way is to declare the script in the header referencing to a script file on your host. I have used scripts before that had a specific purpose that are declared in the body tag but as far as im aware, its rare. You can place the script statement anywhere between the <head> and </head> tags and it won't make a difference.
Its all about when you want the script to load. When your html file loads the body is executed. So as the page loads the script runs. By placing it in the header the script does not run until you trigger it. It is better practice to use the script declaration in the header tag. Also, as i was saying, you may want to get into the habbit of referencing to a script file on your host. <script type="text/javascript" src="yourfile.js"></script>
Script declarations are best put in the header. Script triggers must be in the body at the correct location. Referencing your common script files residing on your host server is good policy as they can be edited without editing every page they occur on. However, some sites, notably eBay, won't allow you to reference offsite scripts, so in those cases your script declarations should be in the header.
It's okay to put your <script> code in the <body>. Takes Google Analytics code as example. However, it is a good practice to put everything in the <head> section.