SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    calif
    Posts
    723
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with validator.w3 instructions

    I'm checking my site with the validator.w3 site, and it shows errors and instructions to fix the errors. can you help me with these two?

    What should this line of code look like corrected, please?
    Validator code & instructions:
    Code:
     (var i =0; i < cb.elements.length; i++)
    You tried to include the "<" character in your page: you should escape it as "&lt;"


    What does this line of code need?
    Validator code & instructions:
    Code:
    <img src="themes/default/images/logo.jpg" border="0" />
    The attribute given above is required for an element that you've used, but you have omitted it. For instance, in most HTML and XHTML document types the "type" attribute is required on the "script" element and the "alt" attribute is required for the "img" element.

    Typical values for type are type="text/css" for <style> and type="text/javascript" for <script>.


    thanks

  2. #2
    In memoriam gold trophysilver trophybronze trophy Dan Schulz's Avatar
    Join Date
    May 2006
    Location
    Aurora, Illinois
    Posts
    15,495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Put the JavaScript in a separate file, or at the very least enclose it with comment tags. As for the second issue, remove border="0" and replace it with img { border: 0; } in your CSS file.

  3. #3
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    calif
    Posts
    723
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply, but I don't know what you mean when you say:

    "Put the JavaScript in a separate file, or at the very least enclose it with comment tags."

    Can you clarify?

  4. #4
    In memoriam gold trophysilver trophybronze trophy Dan Schulz's Avatar
    Join Date
    May 2006
    Location
    Aurora, Illinois
    Posts
    15,495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Without looking at the full code, the most I can tell you is it appears you have some JavaScript that's not only embedded in the HTML, but also unescaped.

    While not syntactically correct for XHTML, the easiest way to get around it (without putting the script in a .js file and linking to it) would be to surround the entire block of JavaScript with an HTML comment, like so:

    Code:
    <script type="text/javascript">
    <!--
    (some JavaScript code...)
    
    (var i =0; i < cb.elements.length; i++)
    
    (some more JavaScript code...)
    -->
    </script>

  5. #5
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ChrisjChrisj View Post
    You tried to include the "<" character in your page: you should escape it as "&lt;"
    That sounds like you're using an XHTML doctype declaration. In that case, you could do exactly as the validator says: escape the '<' as &lt; (or &#38;#60;).

    But ...

    ... if you are serving that XHTML markup as text/html you mustn't do that, because it won't work.

    You can circumvent this with a complicated combination of SGML comments and CDATA sections, but the best way is to place the script in a separate file and include it. That works for both XHTML and HTML (including pretend-XHTML).

    If you use XHTML markup you must not surround the script code with SGML comments, as Dan suggested. That means it won't work if served as real XHTML, which is a Bad and Harmful Practice.


    Quote Originally Posted by ChrisjChrisj View Post
    Code:
    <img src="themes/default/images/logo.jpg" border="0" />
    The attribute given above is required for an element that you've used, but you have omitted it.
    The alt attribute is required for images. It should contain the text equivalent of the image, e.g.,
    Code:
    <img src="themes/default/images/logo.jpg" alt="The Company"/>
    The border attribute is presentational and should be replaced by CSS, as Dan said.
    Birnam wood is come to Dunsinane

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,600
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Don't try to validate CSS or JavaScript as HTML or XHTML - keep them in separate files.

    Surrounding your JavaScript in <!-- --> is an instruction to any CMS that you are using (or to XHTML if your page is defined that way) that the contents are a comment and should be ignored. If you really want that then deleting the code is easier. That sort of comment should NEVER be used with JavaScript unless you require the page to work with Internet Explorer 2 ( or browsers that were around before that browser came out).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm guessing you're using an XHTML-transitional doctype if it didn't mention anything about the "border" attribute in the message for your second one there and you're getting the message about the < in the first.

    What I do to prevent the first, is enclose all of my Javascript in <![CDATA[]]> containers.

    Code:
    <script type="text/javascript">//<![CDATA[
    for(var i=0; i < somevar; i++)
    //]]></script>
    The javascript double forward-slash comments are used to prevent the CDATA containers from being read as syntax errors by the Javascript parser. The XML parser will see those CDATA containers and treat their entire contents as one large string without trying to parse that string.

    For the second message, if you are using a transitional doctype, you should be able to get rid of that error by adding an "alt" attribute to that image like it suggests.

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,600
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by joebert View Post
    The javascript double forward-slash comments are used to prevent the CDATA containers from being read as syntax errors by the Javascript parser. The XML parser will see those CDATA containers and treat their entire contents as one large string without trying to parse that string.
    If you Are really using XHTML then the cdata tags will not get passed to the JavaScript at all and so commenting them out is innecessary. If you are not using XHTML then the JavaScript is considered to be cdata anyway and so the entire cdata tags are unnecessary.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  9. #9
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep I was mistaken. I just double checked myself and it's Googles XML validator I use that for, not the W3C validator.

  10. #10
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't try to validate CSS or JavaScript as HTML or XHTML - keep them in separate files.
    Not always an option, or the best solution.

    If you have a mostly informational site with a lot of people getting directly to what they're looking for through search engines you can end up with a lot of "one page javascript". It's better to have everything on one page and save an HTTP request or three than it is to use external files in those situations.

  11. #11
    SitePoint Member
    Join Date
    Mar 2009
    Location
    Bucharest
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what html editor do you use? try dreamweaver. never forget to check for errors before uploading a file.

  12. #12
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,600
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by joebert View Post
    Not always an option, or the best solution.
    Polluting your HTML with CSS or JavaScript is NEVER required - keeping them separate is ALWAYS the better option. It is always possible to keep them separate unless you are using some prebuilt framework that doesn't allow you to write the HTML properly - in which case there's no point worrying about validation errors.

    Anyway if an HTML validator reports an error inside code that isn't HTML then it hasn't found an error anyway since it is only able to validate HTML. If an inspection on your car reported that the front wall of your house was the wrong colour would you bother getting work done on your car to fix it?
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  13. #13
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry felgall, you're wrong.

    It's like Communism, looks good on paper, but just doesn't work in the real world. I used to think the same thing you're thinking about it.

  14. #14
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,600
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by joebert View Post
    Sorry felgall, you're wrong.

    It's like Communism, looks good on paper, but just doesn't work in the real world. I used to think the same thing you're thinking about it.
    So give an example of some CSS or JavaScript that only works if you place it in the HTML and which can't be done using an external CSS or JavaScript file in order to prove I am wrong then.

    I don't believe you can provide such an example because no such code exists. Every instance I have found can be done simply and much cleaner using separate files.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

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