Why wont this validate?

Hello all,

I am trying to get a page to validate that has a javascript print function for just printing certain content. hear is what the function looks like:


<!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=iso-8859-1" />
<title>mytitle</title>


<script language="JavaScript" type="text/javascript">
var gAutoPrint = true; // Tells whether to automatically call the print function

function printSpecial()
{
	if (document.getElementById != null)
	{
		var html = '<HTML><head><title>CS Stucco and Plaster</title>';

		if (document.getElementsByTagName != null)
		{
			var headTags = document.getElementsByTagName("head");
			if (headTags.length > 0)
			{
				html += headTags[0].innerHTML;
			}
		}

		html += '\
</head>\
<body>\
';
		var printReadyElem = document.getElementById("printReady");

		if (printReadyElem != null)
		{
			html += printReadyElem.innerHTML;
		}
		else
		{
			alert("Could not find the printReady function");
			return;
		}

		html += '\
</body>\
</HTML>';

		var printWin = window.open("","printSpecial");
		printWin.document.open();
		printWin.document.write(html);
		printWin.document.close();
		
		if (gAutoPrint)
		{
			printWin.print();
		}
		else
		{
			alert("The print ready feature is only available if you are using an browser. Please update your browswer.");
		}
	}
}
</script>

and here is the validation errors I get with this setup:

#  Error  Line 17, Column 20: element "HTML" undefined

		var html = '&lt;HTML&gt;&lt;head&gt;&lt;title&gt;CS Stucco and Plaster&lt;/title&gt;';

You have used the element named above in your document, but the document type you are using does not define an element of that name. This error is often caused by:

* incorrect use of the "Strict" document type with a document that uses frames (e.g. you must use the "Frameset" document type to get the "&lt;frameset&gt;" element),
* by using vendor proprietary extensions such as "&lt;spacer&gt;" or "&lt;marquee&gt;" (this is usually fixed by using CSS to achieve the desired effect instead).
* by using upper-case tags in XHTML (in XHTML attributes and elements must be all lower-case).
# Error Line 17, Column 63: character data is not allowed here

		var html = '&lt;HTML&gt;&lt;head&gt;&lt;title&gt;CS Stucco and Plaster&lt;/title&gt;';

Any Ideas how I can fix this??

It depends on the purpose of the page. This is definitely a problem with text in the script. If this is a test, then don’t worry about not validating at the moment, the script won’t cause problems in the production version when the script isn’t inline like that.

If this is the production version, then this is exactly the kind of errors that crop up from inline scripts. You want to yank the script out to avoid this (and many other) problems.

Just copy the whole thing to a new file and save it with a .js extension. Delete the script tags and then insert a link tag at the bottom of the page…


<script type="text/javascript"  src="path/to/myScript.js"></script>

Then you just load the events manually or call a framework function (like jQuery) to automate that for you.

try commenting out your javascript.