Forum Moderators: open
I have a piece of javascript that is causing my xhtml 1.0 transitional page to not validate with the wc3 validator.
Here is the code that is in the <head> section:
document.write('<style type="text/css">\n');
document.write('.submenu{display: none; }\n');
document.write('</style>\n');
And here is the error it causes:
Error Line 14 column 42: document type does not allow element "style" here.
document.write('<style type="text/css">\n');
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).
-=----------------------------
Does javascript usually cause validation errors and is it possible to use javascript and validate? I have other errors but thought i would start with this one. It really needs to validate because it is a uni assignment and that is one of the requirements.
Any help would be greatly appreciated.
Thanks :)
For some reason, in document write, the ending tag's slash needs to be escaped with a backslash (I think). I think that the w3c validator might say this.
Also, you of course, have the document.write statements between <script> and </script> tags, of course.
But I'm new to xhtml 1.0 so I may be wrong. Also, I have never tried to execute a document.write from within the <head> section.
[edited by: lmo4103 at 3:29 pm (utc) on Oct. 4, 2006]