Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validation choking on img tag in javascript

Status
Not open for further replies.

bnooch

Programmer
Mar 16, 2006
4
US
Hello,
I am trying to make my html validate against XHTML 1.0 Transitional.

The validator is choking on this piece of code
Code:
  <div id="hpMain">
<p>
<script type="text/javascript">
var messpic = new Array(
[COLOR=red]"<img id='homepagePic' src='/images/homepage1.jpg' alt='Main Home Page Image' border='0' />",[/color]
"<img id='homepagePic' src='/images/homepage2.jpg' alt='Main Home Page Image' border='0' />",
"<img id='homepagePic' src='/images/homepage3.jpg' alt='Main Home Page Image' border='0' />",
"<img id='homepagePic' src='/images/homepage4.jpg' alt='Main Home Page Image' border='0' />",
"<img id='homepagePic' src='/images/home_page5.jpg' alt='Main Home Page Image' border='0' />",
"<img id='homepagePic' src='/images/homepage5.jpg' alt='Main Home Page Image' border='0' />",
"<img id='homepagePic' src='/images/homepage6.jpg' alt='Main Home Page Image' border='0' />",
"<img id='homepagePic' src='/images/homepage7.jpg' alt='Main Home Page Image' border='0' />");
var max = messpic.length;
var num = Math.floor((Math.random() * max));
document.writeln(messpic[num]);
</script>
<noscript>
<img id="homepagePic" src="/images/home_page5.jpg" border="0" alt="Students" align="bottom" />
</noscript>

Its saying:
Code:
Error  Line 44 column 90: document type does not allow element "img" here.

...t='Main Home Page Image' border='0' />",

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

Line 44 is the first element of the messpic array declaration (red in the code above). It appears to not like my img tag and I cannot figure out what it doesn't like about it. Does anyone have any ideas?

The page resides at Any help would be greatly appreciated...
 
Read up about "CDATA" here:


So the answer is to make your script wrapper look like this:

Code:
<script type="text/javascript">
/* <![CDATA[ */

// content of your Javascript goes here

/* ]]> */
</script>

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Worked like a charm, thank you SO much...
 
Hi

bnooch, we have a hierarchy system here, powered by members' gratitude.

When you receive appreciable help for your problem, or just read some good information, you can thank to the poster by clicking the

* [navy]Thank ?????
for this valuable post![/navy]


link below his message. Then click again on the page which will appear in a pop-up window, to confirm the reward.

So beside thanking SO much, please give him/them a star.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top