Forum Moderators: open

Message Too Old, No Replies

javascript css won't validate

javascript css won't validate

         

colinder

4:32 pm on Nov 13, 2003 (gmt 0)

10+ Year Member



I have oversimplified this to the point of no return, and cannot get it to validate. (http://validator.w3.org)

If you can spot the problem, I’d really like to know what I’m doing wrong. It works fine if I don't use Javascript, but I need to.

----- Here's an example -----

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>won't validate</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
<!--
document.write( '<style type=\"text/css\">{ color:red; }</style>' )
//-->
</script>
</head>
<body>
<h1>This text is red.</h1>
</body>
</html>

----- here's the error -----
Line 8, column 69: end tag for element "STYLE" which is not open

korkus2000

4:41 pm on Nov 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld colinder,

Have you tried removing the html comments from the script tags? Also try making it this in your write:

document.write( '<style type=\"text/css\">{ color:red; }</st' + 'yle>' )

Edouard_H

4:44 pm on Nov 13, 2003 (gmt 0)

10+ Year Member



A backslash in front of the forward slash in your closing tag should do the trick.

document.write( '<style type="text/css">{ color:red; }<\/style>' )

colinder

4:51 pm on Nov 13, 2003 (gmt 0)

10+ Year Member



Greetings, and thanks to the both of you for your speedy advice. I've been looking at that line for hours.

Escaping the slash worked.

One more quick question, though.

Why is it necessary to escape the slash? Does Javascript enterpret it to mean something?

dcrombie

5:29 pm on Nov 13, 2003 (gmt 0)



No, in JavaScript "/" and "\/" are the same animal. The extra '\' stops the validator from seeing the </style> as an HTML tag. So does the other option of splitting the tag and using a '+'.

Edouard_H

5:30 pm on Nov 13, 2003 (gmt 0)

10+ Year Member



Although browsers don't have a problem with it, the </ technically closes the <script> tag.

colinder

5:35 pm on Nov 13, 2003 (gmt 0)

10+ Year Member



I'd like to thank everyone for their intelligent answers.

Mouchos appreciados,
colinder