1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Javascript and XHTML

Join the Discussion

Questions? Comments?

With HTML pages on the web you can just include the required Javascript between <script> and </script> tags. When you validate the HTML on your web page the Javascript content is considered to be CDATA (character data) that is therefore ignored by the validator. The same is not true if you follow the more recent XHTML standards in setting up your web page. With XHTML the code between the script tags is considered to be PCDATA (parsed character data) which is therefore processed by the validator.

Because of this, you can't just include Javascript between the script tags on your page without 'breaking' your web page (at least as far as the validator is concerned).

To fix this problem wer can do one of two things. The simplest way, particularly if the Javascript contains more than just one or two lines, is to make the Javascript external to the page resulting in their being nothing between the script tags to stop the page validating.

If it is just one or two lines then it is probably not worth making an external script so you will want to leave the content between the script tags and tell the validator that this is to be ignored. We do this by placing the Javascript code within a CDATA tag like this:

<script type="text/javascript">
// content of your Javascript goes here

This fixes the validator. The problem is that some older web browsers don't understand the CDATA tag and so this breaks your Javascript in those web browsers. Fortunately, Javascript multi-line comments start and finish with characters that do not cause validation errors. We can therefore comment out the CDATA tag in so far as the Javascript processors within the web browsers are concerned while still leaving the Javascript content defined as CDATA as far as the validator is concerned. We do it like this:

<script type="text/javascript">
/* <![CDATA[ */
// content of your Javascript goes here
/* ]]> */

Now our Javascript code is handled correctly both by newer browsers and validators that actually understand the XHTML DTD as well as by older web browsers that don't understand XHTML and interpret everything on the page as HTML.

One final point about surrounding Javascript embedded in a web page. You will find a lot of code where the Javascript is coded like this:

<script type="text/javascript">
<!-- // hide from really old browsers that noone uses anymore
// also hide from browsers that use the XHTML DTD
// content of your Javascript goes here
// -->

That coding was used by what are now rather old web browsers in order to hide the Javascript content from the really early web browsers that didn't understand Javascript and therefore would have written the Javascript out onto the web page. If you use that coding around your Javascript instead of the CDATA tag then the page will also pass validation but for a different reason - as far as the validator and modern web browsers are concerned, everything in between those tags is a comment and should be ignored by the browser. This will therefore have the reverse effect of what it was originally intended to do where your Javascript ends up hidden from the very latest browsers and is only visible to older browsers. No one is using browsers so old as to require this coding for its original purpose but many will soon be using web browsers where this coding causes the scripts to be commented out completely. Avoid using it.

©2017 About.com. All rights reserved.