Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: My javascript code is making my web page invalid

  1. #1
    Join Date
    Feb 2007
    Location
    West Yorkshire, United Kingdom
    Posts
    145
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default My javascript code is making my web page invalid

    Hi, this javascript code that i am using is making my webpage invalid, the W3C html validator says: "end tag for element "DL" which is not open" document.write("</DL>\n");.

    The javascript code is here...
    Code:
    <script language="javascript" type="text/javascript">
    //store the quotations in arrays
    quotes = new Array(6);
    authors = new Array(6);
    quotes[0] = "I have a new philosophy. I'm only going to dread one day at a time.";
    authors[0] = "Charles Schulz";
    quotes[1] = "Reality is the leading cause of stress for those in touch with it.";
    authors[1] = "Jack Wagner";
    quotes[2] = "Few things are harder to put up with than the annoyance of a good example.";
    authors[2] = "Mark Twain";
    quotes[3] = "The pure and simple truth is rarely pure and never simple.";
    authors[3] = "Oscar Wilde";
    quotes[4] = "There's no business like show business, but there are several businesses like accounting.";
    authors[4] = "David Letterman";
    quotes[5] = "Man invented language to satisfy his deep need to complain.";
    authors[5] = "Lily Tomlin";
    //calculate a random index
    index = Math.floor(Math.random() * quotes.length);
    //display the quotation
    document.write("<DL>\n");
    document.write("<DT>" + "\"" + quotes[index] + "\"\n");
    document.write("<DD>" + "- " + authors[index] + "\n");
    document.write("</DL>\n");
    //done
    </script>

  2. #2
    Join Date
    May 2007
    Location
    Canada
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I would suggest that you use "element.innerHTML" instead of "document.write".

    Anyways, you need to close the <DT> and <DD> tags. I'm assuming that you are using xhtml strict. Try that first.

    document.write("<DT>" + "\"" + quotes[index] + "</DT>");
    document.write("<DD>" + "- " + authors[index] + "</DD>");

  3. #3
    Join Date
    Feb 2007
    Location
    West Yorkshire, United Kingdom
    Posts
    145
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    no i am using HTML 4.01 if u want the full html code with the script in is saved as a webpage here.

  4. #4
    Join Date
    May 2007
    Location
    Canada
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Ryan Fitton View Post
    no i am using HTML 4.01 if u want the full html code with the script in is saved as a webpage here.
    Okay, I've seen a lot of wired things and this is one of them. You need a "space" when you are closing the </DL> tag. So basically what you need to have is:

    document.write("</ DL>\n");

    I tried that and it validated fine although the previous warnings are still there. Note that there's a space before "DL".

  5. #5
    Join Date
    Feb 2007
    Location
    West Yorkshire, United Kingdom
    Posts
    145
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    ok thanks for your time

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    This:

    Code:
    </div>
    in a script will almost always cause a validation error. Something like - 'close tag for div tag that is not open'. You can (if it is a part of a string) do:

    Code:
    <\/div>
    And that will fix that. However, the standard way to avoid these types of questionable validation calls is to make your script external. Another thing, what's a <DL> anyway? A 'Disordered List' tag? It may be some tag I'm not familiar with, but if it isn't, it is in itself invalid HTML, and to be avoided.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Dec 2007
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi,

    Theya are 'list' codes, like ul li but they apply to definition lists
    Code:
    <DL> Definition list
    <DT> Definition term
    <DD> Definition definition
    You can close the dt and dd tags though I think that's not required, but the dl tag has to be closed. So if you had two terms to be defined it would be
    Code:
    <DL>
    <DT>term 1
    <DD>definition 1
    <DT>term 2
    <DD>definition 2
    </DL>
    Or you could also do
    Code:
    <DL>
    <DT>
    <DT>
    <DD>
    <DD>
    </DL>
    Ruth

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    OK, cool. So the tags are valid, it's just the closing tags without their slashes escaped that is the problem, or - as I said, make the script external. This (making the script external) will also fix validation 'errors' like 'poorly formed entity' or such for when the javascript logical && is used in code and mistaken by the validator as a broken HTML entity, and potentially countless other little quirks that arise when trying to parse valid javascript as valid HTML.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    The problem isn't that John. The other tags have to be closed as well (regardless of say so's) and they must be closed first.

    Also, DL tags are not fully supported yet, FF1, Opera 8 and IE6 are highest to show any signs of support. It also is only supported in HTML 3.2 and 4.0 (not 0.1) and XHTML1.0. Which may be another problem, along with using XHTML.


    Edit:
    OP may still have to cancel the forward slash, as well as the other closing tags, I am not sure, it would seem logical.

    Alternatively they could also place them inside a paragraph tag <p></p>
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Not so on the tag's (other than the DL tag) needing to be closed, I thought that but, at least in ordinary HTML 4.01 strict, this validates:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    </head>
    <body>
    <DL>
    <DT>term 1
    <DD>definition 1
    <DT>term 2
    <DD>definition 2
    </DL>
    </body>
    </html>
    So, that much is just like ruthc said.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •