View Full Version : Javascript codes not valid XHTML strict

09-11-2008, 11:57 PM
I'm trying to add some javascript to my forms for when people type passwords that they meet my requirements, but when I go to validate the code, it says the symbols like & and < are not valid, yet they're in the javascript. How can I get around that?

09-12-2008, 01:43 AM
load the javascript from an external file, or use an html doctype.

09-13-2008, 10:57 PM
When I put the javascript in a separate file such as javascript.js, would I include:

<script type="text/javascript">

... in the file? Kind of like how you don't include the start and end tags for CSS when creating an external stylesheet.

09-14-2008, 01:26 AM
No, you don't include the <script> tags at the start of the seperate JS file. As long as you reference the file with:

<script type="text/javascript" src="example.js"></script>

...all should be fine. Hope this helps.

09-14-2008, 09:55 AM
If you still need to use embedded codes, under an XHTML Doctype you must isolate them inside CDATA islands:

<script type="text/javascript">
... code here ...

otherwise the browser's XML parser will take some special javascript characters (< > &) as XML mark-ups.

About CDATA:

Note that when applying the character data for javascript, the CDATA mark-up itself must be, on it's turn, "isolated" (commented), for the same reason (avoiding the XML parser):


09-14-2008, 09:58 PM
Wow, I never even knew about CDATA. Thanks! I think I'm going to use that.