SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist winterheat's Avatar
    Join Date
    Aug 2007
    Posts
    508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XHTML 1.0 strict takes javascript's i < 10 as a beginning of a tag

    just wondering how come a page that is HTML 4.01 doctype can validate and when in XHTML 1.0 strict then it starts to complain the Javascript's i < 10, think the "<" is the beginning of a tag? many thanks.

    http://www.0011.com/test/test_pseudo2.html

  2. #2
    SitePoint Zealot Rexibit's Avatar
    Join Date
    Jul 2008
    Location
    United States
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTML 4.01 uses different rules for validating. It is also more lenient on certain tags. XHTML 1.0 strict requires that your code uses pure XML for the most part. So, your tags must use correct parameters.

    Firstly, you shouldn't use multiple lines to declare your Doctype. It throws up validation errors. Next, the reason that your Javascript was being parsed by the validator was because you didn't hide it.

    You can view the corrected code here: hXXp://www.rexibit.com/development/Untitled-1.htm (just replace the XX with tt). I don't want that page getting indexed lol.

    Another thing to note is that you don't need to declare a "Var" in the beginning of a for statement since it is declared by the nature of the loop.
    Rexibit Web Services
    Don't just build it - CSS it

  3. #3
    SitePoint Evangelist winterheat's Avatar
    Join Date
    Aug 2007
    Posts
    508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah thanks rexibit

    the reason i used that 2-line doctype is from

    http://www.w3.org/QA/2002/04/valid-dtd-list.html

    could it be 1 line or 2 lines both ok? by the way, hiding javascript code? I thought that was the really old style way for old browsers... XHTML is quite modern and it still requires hiding of javascript code? thanks.

  4. #4
    SitePoint Zealot Rexibit's Avatar
    Join Date
    Jul 2008
    Location
    United States
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I generally put all my scripts in an external file, but that is what I do if they are on the page itself.
    Rexibit Web Services
    Don't just build it - CSS it

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    When you put JavaScript inside an XHTML document it must be inside a <![CDATA[ ]]> tag so that it is identified as data and not as part of the XHTML.

    <script type="text.javascript"><![CDATA[
    // javascript goes here
    ]]></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="^$">

  6. #6
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The script element type has very different content models in HTML4 and XHTML1, and that's what's causing this problem.

    In HTML, the content model is CDATA, which is parsed with very special rules. Character entity references are not parsed, and the first occurrence of '</' (followed by a name start character) terminates the script element.

    In XHTML, the content model is (#PCDATA). Entity references are parsed, '<' is recognised as a TAGO delimiter, etc.

    In XHTML you should write i &lt; 10 in your script code, but that won't work if you use pretend-XHTML since that is really HTML, which means the entity reference won't be parsed.

    There is no good way to have the cake and eat it too, so with XHTML (pretend or real) you definitely should keep JavaScript in external files. Of course, that's best practice for HTML as well.
    Birnam wood is come to Dunsinane

  7. #7
    SitePoint Guru Chroniclemaster1's Avatar
    Join Date
    Jun 2007
    Location
    San Diego, CA
    Posts
    784
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, all these nicities just show why your javascript should be in an external file. As you begin to build more complicated pages, maybe even get into server side programming, the sheer size of the pages becomes daunting, especially when you're trying to read multiple languages. Keeping javascript, and CSS in separate files, let you isolate all your HTML (structure and content), CSS (appearance), and javascript (behavior). So when you need to maintain your page later, it's so much easier to locate and fix the problem. Especially, if you have moved on and someone else is trying to maintain the site.

    Be kind, use code separation...
    Whatever you can do or dream you can, begin it.
    Boldness has genius, power and magic in it. Begin it now.

    Chroniclemaster1, Founder of Earth Chronicle
    A Growing History of our Planet, by our Planet, for our Planet.

  8. #8
    SitePoint Evangelist winterheat's Avatar
    Join Date
    Aug 2007
    Posts
    508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for all your help. yes I think if possible and if it is for production website, then I will use external js files. it can speed up the loading of the page... and i think some performance book says it is best to put the link at the end, so that the content gets displayed first (and thus faster display). usually as an example, i like to put everything (html, js, css) in one file so that we can look at it and cross reference the all in one file.


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
  •