Forum Moderators: open

Message Too Old, No Replies

My html will not validate with an anchor declared in header section

         

wgoldman

1:08 am on May 4, 2010 (gmt 0)

10+ Year Member



I am not sure why this throws a validation error and/or what to do about it. The is no self closing tags that I can see. I do not have a style element in the body. Help is appreciated.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
<title>My title page</title>
<script language="javascript" type="text/javascript">
var myAnchorVar = '<a href="#">IMA ANCHOR USED IN JAVASCRIPT</a>';
</script>
</head>
<body>
</body>
</html>

Produces this validation error;
Line 7, Column 32: document type does not allow element "a" here
var myAnchorVar = '<a href="#">IMA ANCHOR USED IN JAVASCRIPT</a>';
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).

incrediBILL

1:36 am on May 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The simplest solution is to put your javascript in HTML comments so it's ignored by the validator but it still runs just fine.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
<title>My title page</title>
<script language="javascript" type="text/javascript"><!--
var myAnchorVar = '<a href="#">IMA ANCHOR USED IN JAVASCRIPT</a>';
--></script>
</head>
<body>
</body>
</html>


It's an old Jedi trick... "you do not see the javascript..."

jdMorgan

1:36 am on May 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The validator fails to take into account that the "a" element is enclosed both in quotes and within a <script> element... So it's a poor validator, basically.

If 100% validation is important to you, and if you actually have several more lines of <script> code not shown here, consider moving all of it to an external file and including that file instead of coding it in-line.

Jim

Fotiman

1:12 pm on May 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



We just had this discussion over in the JavaScript forum:
[webmasterworld.com...]

Note, putting your scripts in an external file is the best solution. I would recommend against using HTML comments within <script></script> tags. If you're going to include JavaScript in your XHTML page, the the proper method is to put it in a CDATA section (discussed in the thread I linked to). But again, if you keep your script out of your markup, then you won't have to worry about this.

g1smd

10:31 pm on May 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Putting it in an external file also hides it from many bots that would otherwise 'extract' it from the HTML and then try to follow it.

wgoldman

10:51 pm on May 4, 2010 (gmt 0)

10+ Year Member



All excellent suggestions. I ended up putting it into a separate file and including that file. The motivation for including it was due to some of the text in the javascript might be needed for search engine reasons, however, the pieces that were inside html tags were not that important for those reasons.