SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict ozmart2004's Avatar
    Join Date
    Feb 2004
    Location
    Albury, NSW, Australia
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can't validate html tags inside javascript

    Hi

    I am trying to validate some <br /> tags inside some javascript.

    Code:
    Line 104, column 52: document type does not allow element "br" here
    
    document.write("Browser identifier: "+br[0]+"<br />");
    
    The element named above was found in a context where it is not allowed. This could mean that you have incorrectly nested elements -- such as a "style" element in the "body" section instead of inside "head" -- or two elements that overlap (which is not allowed).
    
    One common cause for this error is the use of XHTML syntax in HTML documents. Due to HTML's rules of implicitly closed elements, this error can create cascading effects. For instance, using XHTML's "self-closing" tags for "meta" and "link" in the "head" section of a HTML document may cause the parser to infer the end of the "head" section and the beginning of the "body" section (where "link" and "meta" are not allowed; hence the reported error).
    I've tried using the recommended escapes "\" in various places but nothing seems to work. Any ideas?

    Thks.

  2. #2
    SitePoint Guru Ruben K.'s Avatar
    Join Date
    Jun 2005
    Location
    Alkmaar, The Netherlands
    Posts
    693
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't have an inline script?

    Code:
    <script type="text/javascript" src="main.js" /></script>
    This, maybe?

  3. #3
    bronze trophy
    Join Date
    Dec 2004
    Location
    Sweden
    Posts
    2,670
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have processing characters ("<" and "&") in your script in an XML file, you must escape it with a CDATA section, such as:
    Code:
    <script type="text/javascript"><![CDATA[
    
    ]]></script>
    In HTML, SCRIPT is already CDATA. So the string "<![CDATA[" is processed with the javascript engine when you serve your file as text/html, which will cause a javascript error. To get around this problem, you can comment it out, i.e.:
    Code:
    <script type="text/javascript">//<![CDATA[
    
    //]]></script>
    Another thing you should be aware of is that document.write() doesn't work at all in XML. You should use DOM methods instead. (And if you use DOM methods, you probably won't get any processing characters in your script, which means that you don't need CDATA.)
    Code:
    function createElm(e){ // createElementNS for XML and createElement for text/html
     return (document.xmlVersion) ? document.createElementNS('http://www.w3.org/1999/xhtml', e) : document.createElement(e);
    }
    var placeholder = document.getElementById('placeholder');
    placeholder.appendChild(document.createTextNode('Browser identifier: '+br[0]));
    placeholder.appendChild(createElm('br'));
    Simon Pieters

  4. #4
    SitePoint Addict ozmart2004's Avatar
    Join Date
    Feb 2004
    Location
    Albury, NSW, Australia
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thks zcorpan

    Javascript is not really my thing but I think I need to brush up on DOM Methods.....

  5. #5
    bronze trophy
    Join Date
    Dec 2004
    Location
    Sweden
    Posts
    2,670
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unobtrusive Javascript is a good read.
    Simon Pieters


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
  •