PDA

View Full Version : My javascript code is making my web page invalid



Ryan Fitton
12-30-2007, 11:48 AM
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...

<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>

pman
12-30-2007, 04:14 PM
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>");

Ryan Fitton
12-30-2007, 04:21 PM
no i am using HTML 4.01 if u want the full html code with the script in is saved as a webpage here (http://h1.ripway.com/ryanfitton/index.htm).

pman
12-30-2007, 04:56 PM
no i am using HTML 4.01 if u want the full html code with the script in is saved as a webpage here (http://h1.ripway.com/ryanfitton/index.htm).

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".

Ryan Fitton
12-30-2007, 05:09 PM
ok thanks for your time :)

jscheuer1
12-30-2007, 05:12 PM
This:


</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:


<\/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.

ruthc
12-30-2007, 06:24 PM
Hi,

Theya are 'list' codes, like ul li but they apply to definition lists

<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

<DL>
<DT>term 1
<DD>definition 1
<DT>term 2
<DD>definition 2
</DL>

Or you could also do

<DL>
<DT>
<DT>
<DD>
<DD>
</DL>
Ruth

jscheuer1
12-30-2007, 06:39 PM
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.

BLiZZaRD
12-30-2007, 07:00 PM
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.



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>

jscheuer1
12-30-2007, 07:19 PM
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:


<!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.

ruthc
12-30-2007, 08:15 PM
Well, I'm not up on XHTML1.0 or 1.1 but it will validate for both

XHTML1.0

<?xml version="1.0" encoding="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>xhtml1.0 and dl dt dd</title>
</head>
<body>
<dl>
<dt>term 1</dt>
<dd>definition 1</dd>
<dt>term 2</dt>
<dd>definition 2</dd>
</dl>
</body>
</html>

and using this doctype for 1.1 also validates

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

Though there is a warning about conflict between mime type and doctype, something about using application/xhtml+xml or something.

Ruth

jscheuer1
12-31-2007, 05:05 AM
If you have an XHTML DOCTYPE and an xml prologue, the mime type should be application/xml or some such thing. This is controlled by the server though. When validating a local page, it will be seen as mime type text/html by the validator.

In almost all cases though HTML 4.01 Strict should be used. The only exception being if you need to be serving XHTML/xml. And, if you do serve it, that leaves out IE.

ruthc
12-31-2007, 06:29 AM
Well, since I don't even know what it is, I think I'll stick to coffee :D

lol, I'm having a silly moment.

Ruth