search engine optimization articles do it yourself seo hiring search engine experts html and search engine optimization web site analysis and submission service

HTML Help: Comment Out JavaScript

Someone told me I should put my JavaScript in between comment tags. Is this true? What's the purpose?
It used to be that JavaScript support was very spotty at best, and browsers that didn't support JavaScript would often display the code on the page and make a mess of things.

The way around that was to use HTML comment tags to hide the JS from older browsers, thus ensuring backward compatibility. A typical JavaScript with comment tags would look like this:

<script type="text/javascript">
<!--
   The actual script would be here.
-->
</script>
When CSS came along, the same technique was used to hide the CSS from old browsers so they wouldn't display the CSS code on the page. That "trick" worked for CSS too, and everyone lived happily ever after.

Um . . . not quite.

Say goodbye to all that. Using this traditional "trick" means you may now be hiding your JavaScript and CSS from NEW browsers!

XML compliant browsers, which is most any current version browser, are supposed to ignore content within comment tags. Here's the text from the official W3C specs on Embedded Style Sheets and Scripts (section C.4):

Note that XML parsers are permitted to silently remove the contents of comments. Therefore, the historical practice of "hiding" scripts and style sheets within "comments" to make the documents backward compatible is likely to not work as expected in XML-based user agents.
I've used that trick on my sites in the past, but no more. There is only a very small percentage of old browsers still in use that wouldn't be compatible nowadays, so the trick is not really needed anymore.

The choice between being backward compatible for first and second generation browsers or being forward compatible with current and future browsers seems obvious to me. Since the number of incompatible browsers in use is next to nothing nowadays, I suggest we move forward.

This concludes the

HTML Help about Hiding JavaScript in HTML Comment Tags

.
Back | HTML FAQ | HTML Tag Chart | HTML Index | Web Design Tutorials
search engine optimization course
If you want your web site to rank high in the search engines . . . what are you going to do to get it there? Check out my search engine optmization guide, SEO for YOU: Search Engine Optimization for Ordinary Everyday People!

Check out SEO for YOU now!

Almost a Newsletter

Subscribe today for exclusive website design tutorials and grab some free gifts to boot! Learn more, or subcribe below:
Email:

First Name:

Privacy Policy

Did you know...

The member's site has about 100 standards compliant HTML and CSS tutorials, 31 handy reference charts, reprintable content, web graphics, exclusive fonts, free software, free ebooks and more? All this for less than 9 cents a day! [ Details ]
See my fancy bottom! :)