For those who insert javascript code in pages and want to validate as XHTML: 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.

To fix this problem we 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 you want to keep Javascript code inside your page then place the Javascript code within a CDATA tag like this:

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

What is important here is to place CDATA tags in between /* */ chars. 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.

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.

3 Responses to “Validating Page with Javascript Code Inside”

  1. Kalinda  

    Hi all. A rumor without a leg to stand on will get around some other way. Help me! It has to find sites on the: Round bathroom mirrors. I found only this - debt relief form letters. So there it is; one mirror renovation for the low, low cost of. Another creative example is of a mirror with a pink glass background. With respect :mad:, Kalinda from Micronesia.

  2. 1 Bel  

    Hi. If you watch a game, it’s fun. If you play at it, it’s recreation. If you work at it, it’s golf. Help me! Could you help me find sites on the: F1 betting odds. I found only this - Betting calculator odds and evens. Jermain defoe to score two or more goals. The november nine is set for the wsop final table on november th, with the chip leader being a logger from maryland, darvin moon. Thank :cool: Bel from Madagascar.

  3. 2 Rawdon  

    Greeting. In real life, unlike in Shakespeare, the sweetness of the rose depends upon the name it bears. Things are not only what they are. They are, in very important respects, what they seem to be.
    I am from Tonga and learning to write in English, give true I wrote the following sentence: “In the meantime, make a mad dash to to get off your entire purchase.”

    Regards :P Rawdon.

Leave a Comment

This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)

You must read and type the 5 chars within 0..9 and A..F, and submit the form.

  

Oh no, I cannot read this. Please, generate a