...

View Full Version : Javascript codes not valid XHTML strict



nicky
09-11-2008, 10: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?

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

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



<script type="text/javascript">
</script>


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

urbanbutterfly
09-14-2008, 12: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.

Kor
09-14-2008, 08: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">
/*<![CDATA[*/
... code here ...
/*]]>*/
</script>

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

About CDATA:
http://en.wikipedia.org/wiki/CDATA
http://www.w3schools.com/XML/xml_cdata.asp

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):

/*<![CDATA[*/
...
/*]]>*/

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum