Forum Moderators: mack

Message Too Old, No Replies

W3c Validating error

         

kernoman

10:29 am on Jan 4, 2006 (gmt 0)

10+ Year Member



Hi,

I have a page which has the follwoing piece of javascript in it to handle expanding and collapsing a unordered list that i am going to use as a menu however when I validate the code it states that 'document type does not allow element "img" here'

Here is the relevant bits of code minus the javascript:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text-html; charset=utf-8" />
<title>Expanding Menu</title>
</head>
<body>
<ul>
<li>
<script type="javascript">document.writeln('<img id="bar1" src="closed.png" alt="Open list" onclick="toggle(\'bar1\',\'section1\');" />');></script>
<a href="javascript:toggle('bar1','section1')">Section 1</a>
</li>
</ul>

<ul id="section1">
<li><a href="page1.html">Item 1</a></li>
<li><a href="#">Item 2</a> </li>
<li><a href="#">Item 3</a> </li>
<li><a href="#">Item 4</a> </li>
</ul>
</body>
</html>

The error relates to the line:
<script type="javascript">document.writeln('<img id="bar1" src="closed.png" alt="Open list" onclick="toggle(\'bar1\',\'section1\');" />');></script>

Why can I not put an img tag there?

Any help appreciated

zCat

10:35 am on Jan 4, 2006 (gmt 0)

10+ Year Member



Probably you need to wrap the script in a CDATA section:

<script type="javascript">
<![CDATA[
... your script ...
]]>
</script>

kernoman

10:50 am on Jan 4, 2006 (gmt 0)

10+ Year Member



Thank you! that worked, I have never heard of the cdata tag before.

kernoman

11:59 am on Jan 4, 2006 (gmt 0)

10+ Year Member



One question though, will this have any knock on affect with the img id not being available to my javascript?

kernoman

12:32 pm on Jan 4, 2006 (gmt 0)

10+ Year Member



CDATA appears to make the browser skip the javascript specified within the CDATA tag, is there a way to get over this?

kernoman

1:16 pm on Jan 4, 2006 (gmt 0)

10+ Year Member



Responding to my own post again however in case anyone else ever has this problem the way I resolved it was to use this format:

<script type="text/javascript"><!--//--><![CDATA[//><!--
...
//--><!]]></script>