a validator warning message?

Discussion in 'HTML & Website Design' started by mahmood, Feb 4, 2006.

  1. #1
    Hi

    I have a javascript in my document whic contains "<" like this:

    
    if(document.getElementById('user_comment').value.length < 10)
    
    PHP:
    even though it is inside an script tag still xhtml gives me a warning. What do I do then?
     
    mahmood, Feb 4, 2006 IP
  2. Skeleton

    Skeleton Peon

    Messages:
    116
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you want to validate XHTML, you must use this style for your JavaScripts:

    
    <script type="text/javascript">
    //<![CDATA[
    
    alert("<This is compatible with XHTML>");
    
    //]]>
    </script>
    
    HTML:
    via Making JavaScript Compatible with XHTML
     
    Skeleton, Feb 4, 2006 IP
  3. AWD1

    AWD1 Peon

    Messages:
    191
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Alternatively, you could externalize your code snippet into a Javascript file so that it's separate from your output code.

    Something like this in your <head> tag:
    
    <script type="text/javascript" src="form_validation_by_mahmood.js">
    <!--
    <![CDATA[
    ]]>
    -->
    </script>
    
    Code (markup):
    form_validation_by_mahmood.js would contain your function.

    From there, you'd just call your function the same as you normally would (assuming you put your JScript into a function.)

    Personally, I recommend doing this, since it separates your Javascript output from your HTML output, thus making it easier to debug. I'm also not sure, but I remember reading somewhere that JScripts tend to perform slightly more efficiently this way, especially in situations where the JScript can be cached.
     
    AWD1, Feb 5, 2006 IP