Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post

    Javascript codes not valid XHTML strict

    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?

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    725
    Thanks
    35
    Thanked 132 Times in 123 Posts
    load the javascript from an external file, or use an html doctype.

  • #3
    Regular Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    When I put the javascript in a separate file such as javascript.js, would I include:

    Code:
    <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.

  • #4
    New Coder
    Join Date
    Aug 2008
    Location
    Essex, UK
    Posts
    39
    Thanks
    7
    Thanked 0 Times in 0 Posts
    No, you don't include the <script> tags at the start of the seperate JS file. As long as you reference the file with:

    Code:
    <script type="text/javascript" src="example.js"></script>
    ...all should be fine. Hope this helps.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    If you still need to use embedded codes, under an XHTML Doctype you must isolate them inside CDATA islands:
    Code:
    <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[*/
    ...
    /*]]>*/
    Last edited by Kor; 09-14-2008 at 09:00 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    Regular Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    Wow, I never even knew about CDATA. Thanks! I think I'm going to use that.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •