|
HTML Tip:
Beware of Stray Thoughts In Your HEAD
by Tom Dahm,
Chief Operations Officer,
NetMechanic, Inc.
If you follow good HTML coding conventions, then your Web page probably has a HEAD section. Be careful how you structure that section, though, because stray text inside your HEAD can break your page under Microsoft Internet Explorer.
The normal structure of an HTML document divides the page into two sections: the HEAD and the BODY. The BODY section contains all the text and tags that you want the browser to display. The HEAD section stores other information that's useful for indexing, classifying or otherwise managing your page. This section usually includes things such as your TITLE tag and one or more META tags. Many Webmasters also put SCRIPT tags inside the HEAD section to store JavaScript code.
The HTML 4.0 standards say that only certain tags should be placed inside the HEAD section. Only the following tags are allowed:
- BASE
- ISINDEX
- LINK
- META
- SCRIPT
- STYLE
- TITLE
|
Putting any other tag inside the HEAD isn't valid HTML and will break your page under Internet Explorer. The same is true if you put plain text outside of an HTML tag inside the HEAD.
For example, look at the code below. This is a normal HTML document, but with stray text placed inside the HEAD section.
<HTML>
<HEAD>
stray text
</HEAD>
<BODY BGCOLOR="yellow" TEXT="red">
This background should be yellow
</BODY>
</HTML>
|
Figure 1 shows this page as viewed using Netscape Navigator 4.02. Figure 2 shows how the page looks under Microsoft Internet Explorer Version 4.72.
In both cases the stray text is displayed on the resulting Web page. But notice that this text also breaks the BODY tag under MSIE. Internet Explorer renders the background color in white, even though we told it to use yellow. We also set the BODY tag to render all text in red, but this is broken too. Stray text inside the HEAD will break any BODY attribute, including BACKGROUND. All of this can make your page look radically different under Navigator and Internet Explorer.
The most common culprit for this sort of thing is JavaScript. Part of the reason for JavaScript's popularity is that it's easy to cut and paste scripts into your page, even if you don't know the programming language. But a badly pasted JavaScript can cause exactly this sort of stray text problem. Be sure that all of your JavaScript code is inside a SCRIPT tag to avoid this problem.
Also remember to run NetMechanic's HTML Check (part of our HTML Toolbox suite) to spot tags that aren't legal inside the document HEAD.
|