Forum Moderators: open

Message Too Old, No Replies

Is this piece of code correct

Javascript after </html>

         

dross

5:59 pm on Oct 25, 2003 (gmt 0)

10+ Year Member



Hi, I'm doing SEO on a site and came across this code and would think that it's incorrect. But before I tell the designer that it's wrong, I wanted to double check. At the bottom of the HTML code, it has this.

</body>
</html>
<script language="JavaScript" src="hits.jsp"></script>

I thought the JavaScript needed to be between the head or body tags.

Thanks
David

MonkeeSage

6:49 pm on Oct 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The <script> block can either be in the <head> or <body>. AFAIK, your not supposed to have anything anywhere outside the root block (<html>), except the DTD immediately before it (and, optionally, the <?xml?> declaration before that).

Jordan

DrDoc

10:19 pm on Oct 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If in doubt, you can always run the page through the markup validator [validator.w3.org]

tedster

11:15 pm on Oct 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thing is, every browser I've tested will run javascript no matter where it appears in the document - before or after the HTML element, in between the </head> and <body> tags, and so on. I've seen lots of this kind of coding coming out of CMS "enabled" organizations.

Of course, properly speaking, the <script> tag is HTML markup, so it belongs within the html element. But practically speaking, it doesn't seem to matter.

However, because the browser will parse the HTML document from top to bottom, this particular positioning you quoted means that the script will be the very last thing to execute. If it really is a hit counter script (as it seems to be from its name), people who bail early from the page won't get counted. In fact, you can get some intersting stats by running the same code as the first thing on the page, and then the last thing.

While you're digging into this area, you might as well have them change the long-time deprecated "language=" attribute. It should read:

<script type="text/javascript" src="hits.jsp"></script>