#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Tennessee
    Posts
    13
    Rep Power
    0

    HTML 4.01 Validation (with Javascript)


    I searched the DEV SHED forums for these topics, but with minimal success.

    I have not been successful with writing Javascript that is intended to write HTML to a window. In the brief example that follows (which was extracted from a program in a textbook), the W3C Validator gave 11 errors (listed below), and all of them appear to be related to the business about concatenating HTML
    information in javascript.

    Here's the code fragment:

    function createWindow()
    {
    secondwindow = window.open('','example','height=300,width=200,scrollbars=yes');
    if (secondwindow != null)
    {
    var windowHTML= "<html><head><title>Second Window</title></head>";
    windowHTML += "<body><h1 align='center'>";
    windowHTML += "Another window!</h1><hr><center><form>";
    windowHTML += "<input type='button' value='Set main red' onclick='window.opener.document.bgColor=\"red\"'>";
    windowHTML += "<br><input type='button' value='CLOSE' onclick='self.close()'>";
    windowHTML += "</form></center></body></html>";
    secondwindow.document.write(windowHTML);
    secondwindow.focus();
    }
    }

    And these are the errors from the W3C validator.



    1. Line 15, column 63: end tag for element "TITLE" which is not open (explain...).

    ...r windowHTML= "<html><head><title>Second Window</title></head>";
    ^

    2. Line 15, column 70: end tag for "SCRIPT" omitted, but its declaration does not permit this (explain...).

    ..."<html><head><title>Second Window</title></head>";
    ^

    3. Line 8, column 0: start tag was here (explain...).

    <script language="JavaScript" type="text/javascript">
    ^

    4. Line 16, column 27: document type does not allow element "BODY" here (explain...).

    windowHTML += "<body><h1 align='center'>";
    ^

    5. Line 17, column 59: required attribute "ACTION" not specified (explain...).

    windowHTML += "Another window!</h1><hr><center><form>";
    ^

    6. Line 20, column 52: character data is not allowed here

    windowHTML += "</form></center></body></html>";
    ^

    7. Line 26, column 8: end tag for element "SCRIPT" which is not open (explain...).

    </script>
    ^

    8. Line 27, column 6: end tag for element "HEAD" which is not open (explain...).

    </head>
    ^

    9. Line 28, column 5: document type does not allow element "BODY" here (explain...).

    <body>
    ^

    10. Line 29, column 5: required attribute "ACTION" not specified (explain...).

    <form>
    ^

    11. Line 36, column 6: end tag for element "HTML" which is not open (explain...).

    Is this a fundamental incompatibility between expectations of javascript and HTML and the realities of the W3C standard, or am I screwing up something with syntax?

    Anyone?

    Thanks,
    Three Amigos here.


    ^

  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Tennessee
    Posts
    13
    Rep Power
    0

    Just a note about that HTML function


    Hey folks;

    I just wanted to say that I don't care anything about the actual function that I posted...I know it works, and how it works, but what I cannot figure out is why the W3C HTML 4.01 Validator does not "bless" it.

    The functions that I wrote (and that the validator choked on) do work in my project (in Netscape at least), but they are nasty-looking, so I chose this function from a textbook (that should have validated-no?).

    Thanks,
    Three Amigos
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    147
    Rep Power
    12
    When writing HTML within javascript you have to escape the '/' character. So instead of
    Code:
    document.write("<a href=\"whatever\">link</a>");
    Use this
    Code:
    document.write("<a href=\"whatever\">link<\/a>");
    Note that </a> becomes <\/a>. So make those changes, revalidate and post any more errors it gives you.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Tennessee
    Posts
    13
    Rep Power
    0

    Thanks


    Thanks

    You know after I posted and came back, I saw a post that another fellow had made from Australia, I think, and I picked up on that escape character necessity from looking at his very last post.

    But, yours was a very precise answer to m y post, and I appreciate it.

    Thanks frm
    Three Amigos

IMN logo majestic logo threadwatch logo seochat tools logo