SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Wizard dethfire's Avatar
    Join Date
    Aug 2000
    Posts
    2,477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    document type does not allow element "script" here

    I am completely baffled why I have tons of xhtml errors in my head part, can anyone educate me?

    http://validator.w3.org/check?uri=ht...Inline&group=0
    Free Science Homework Help
    http://www.physicsforums.com

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    I know from experience that a single error can result in multiple error messages being reported, so don't let the number shock you too much.
    I think at least some errors could be resolved if you used HTML comments inside the javascript tags i.e.
    HTML Code:
    <script type="text/javascript">
    <!--
    var whatever = "abc";
    var etc = "123";
    // -->
    </script>

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Placing comments around JavaScript like that is not needed unless your audience is using Netscape 1 and IE2 and serving the page as HTML. Enclosing in a comment like that in XHTML will have the code treated as a comment and it will not be run.

    For XHTML you should enclose the JavaScript inside a <![CDATA[ ]]> tag in order for it to not be treated as badly coded XHTML.

    Code:
    <script type="text/javascript">
    <![CDATA[
    var whatever = "abc";
    var etc = "123";
    ]]>
    </script>
    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="^$">

  4. #4
    SitePoint Wizard dethfire's Avatar
    Join Date
    Aug 2000
    Posts
    2,477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Placing comments around JavaScript like that is not needed unless your audience is using Netscape 1 and IE2 and serving the page as HTML. Enclosing in a comment like that in XHTML will have the code treated as a comment and it will not be run.

    For XHTML you should enclose the JavaScript inside a <![CDATA[ ]]> tag in order for it to not be treated as badly coded XHTML.

    Code:
    <script type="text/javascript">
    <![CDATA[
    var whatever = "abc";
    var etc = "123";
    ]]>
    </script>
    When I put in the cdata tags the javascript does not work. I am using IE 7.
    Free Science Homework Help
    http://www.physicsforums.com

  5. #5
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dethfire View Post
    I am completely baffled why I have tons of xhtml errors in my head part, can anyone educate me?
    The first error is because you've used an unescaped '<' character in some JavaScript code. The error message tells you exactly how to fix it: escape '<' as '&lt;'.

    Now, this will be a problem for you, because it won't work if you serve the document as text/html (which you most likely do, or you'd get nothing but an error page).

    You really should keep JavaScript external with XHTML (even pretend-XHTML), because the different ways the script element type is define in HTML and XHTML makes it all but impossible to write embedded scripts in a way that works with both.

    Also, you cannot use document.write() with XHTML. That will only work as long as it's served as text/html. If you rely on it being parsed as HTML you really should use HTML, not pretend to use XHTML. This practice really is harmful, because it means your document will not work if served as XHTML.

    I recommend that you read the XHTML vs HTML FAQ.

    Quote Originally Posted by Mittineague View Post
    I think at least some errors could be resolved if you used HTML comments inside the javascript tags
    No, no, no!
    I think you also need to read the XHTML vs HTML FAQ.
    Using SGML comments around the content of the script element will make it a comment in XHTML. It's not like in HTML, where such a comment is ignored for script and style. Such comments are archaic anyway, since virtually no-one uses Netscape 2 any more.
    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,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by AutisticCuckoo View Post
    Such comments are archaic anyway, since virtually no-one uses Netscape 2 any more.
    Netscape 2 doesn't need the comments since it was the first browser to understand JavaScript. It is only Netscape 1 and IE2 (and earlier browsers) that need comments around the script to hide 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="^$">

  7. #7
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I stand corrected. Does Netscape 2 need the comments for style elements, then? I seem to recall that there was something about Nescape 2 and those comments.
    Birnam wood is come to Dunsinane

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    I am not sure but I think the style tag was introduced more recently than the script tag and that Netscape 2 probably needs the comments around your styles.

    Of course the best solution in each case is to put them into separate files and then you don't have to worry about hiding the code at all.
    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
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    Indeed!
    Quote Originally Posted by felgall
    Enclosing in a comment like that in XHTML will have the code treated as a comment and it will not be run.
    Quote Originally Posted by AutisticCuckoo
    No, no, no!
    I think you also need to read the XHTML vs HTML FAQ.
    Most of my javascript is external files. A bit is without any comments, and a very little had the HTML comments (now fixed thanks to this thread).
    Up until very recently, the pages were "fake" XHTML. The pages validate, but as you know, validation isn't everthing. So I did a bit of testing to see how I could have missed this problem before.
    PHP Code:
    <?php
    if (stristr($_SERVER["HTTP_ACCEPT"], "application/xhtml+xml"))
    {
        
    header("Content-Type: application/xhtml+xml; charset=utf-8");
        echo 
    "<?xml version='1.0' encoding='utf-8'?>";
    }
    else
    {
        
    header("Content-Type: text/html; charset=utf-8");
    }
    ?>
    <!DOCTYPE html 
            PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
            'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
    <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
    <head>
    <title>Javascript Test Page</title>
    <?php
    if (stristr($_SERVER["HTTP_ACCEPT"], "application/xhtml+xml"))
    {
        echo 
    "<meta http-equiv='Content-Type' content='application/xhtml+xml; charset=utf-8' />";
    }
    else
    {
        echo 
    "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
    }
    ?>
    <script type="text/javascript" src="external.js"></script>
    <script type="text/javascript">
    alert("Not commented");
    </script>
    <script type="text/javascript">
    <!--
    alert("HTML comments");
    //-->
    </script>
    <script type="text/javascript">
    <![CDATA[
    alert("CDATA comments");
    ]]>
    </script>
    </head>
    <body>
    <p>4 alerts</p>
    <ul>
        <li>external javascript</li>
        <li>uncommented javascript</li>
        <li>HTML commented javascript</li>
        <li>CDATA commented javascript</li>
    </ul>
    </body>
    </html>
    Opera and Firefox alert the "external", "uncommented", and "CDATA" javascript.
    But IE (which doesn't support XHTML) alerted the "external", "uncommented", and "HTML" javascript.

  10. #10
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Presumably IE also gave an error on the CDATA one. To get that to process without giving an error you need to use JavaScript comments to comment out the CDATA tag.

    // <![CDATA[
    // ]]>

    or

    /* <![CDATA[ */
    /* ]]> */
    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="^$">

  11. #11
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    Yes, without the CDATA being commented out, IE gives a "syntax error". But with the CDATA commented out it "works" (same as uncommented). And it still works in Opera and Firefox. I still prefer external javascript files, but it's good to know this if I ever need to do use it.


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
  •