Forum Moderators: open
Take the following code:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>
<head>
<title>XHTML test</title>
</head><body>
<script type="text/javascript">
if ( 2 > 1 && 2 < 5)
{
alert('hi mum');
}
</script></body>
</html>
I just tried it in a validator and it didn't validate because of the >, < and && comparison operators in the javascript. Does this mean you can't have such javascript in an XHTML document or is there something I'm missing? Are we back to the days of putting //<!-- and //--> comments around our scripts?
<script type="text/javascript" src="javascript/menu.js"></script>
Since there is no content to the <script> tag whatsoever, there is no issue with needing to wrap anything in comments or CDATA no matter how old or oddball a browser someone may use, nor whether you are writing HTML or XHTML. It works in Mozilla (and other Gecko browsers) and IE. The script doesn't work in NN4, but that is just as likely to be the javascript itself as how I imported it, I think. It does validate at validator.w3.org.
<script type="text/javascript">
<![CDATA[
alert('hello');
]]>
</script>
I've tried that in IE6, NN6 & 7 and Opera and they all error. I tried the W3C prior to posting and saw the content about the CDATA section - but couldn't get it to work.
Additionally, having to have most of your script in an include can be a total pain if you just want to use some inline script in an event or, more commonly, want to dynamically generate some JS to work closely within a dynamically generated page.
So, unless I'm doing the CDATA thing incorrectly, are comments the way others are getting around this problem at the moment?
added:
I had a look at the source on w3schools.com and saw that they were using the old fashioned script comments:
<script language="javascript" type="text/javascript">
<!-- Hiding from other browsers
...script here...
// -->
</script>
from: [mit.edu...]
Which, as I read it, means that this is recommended:
<script type="text/javascript">
//<![CDATA[
if (h && i) j();
//]]>
</script>
I'll have a good read of that tomorrow. I have to say, I was a little surprised when IE6 and NN7 didn't support the CDATA node - The xhtml2 spec has just been released yet we don't have a browser that fully supports xhtml1 yet. You can't blame 'em for getting these specs out 'in plenty of time'.
I guess the good old days of hacking solutions to these problems are just never gonna go away.
OK, any ideas how you do this in XSLT1 anyone ;)?
There really isn't an easy solution. Encasing scripts and style sheets in comment delimiters (<!-- -->) does not officially work. According to the W3C, the parser may remove all comments before passing the code onto the user agent. In addition, C-like languages, including Javascript, have a decrement operator ("--") that just happens to be the SGML comment delimiter.
Does this throw the "I use XHTML to future-proof my work" argument out the window?
I think combining the two doesn't really make much sense as, in XML terms, there is no longer a CDATA node as it's inside SGML comments tags.
Not in XML terms, but in terms of old browsers who know neither XML nor JavaScript.
And it is OK for today´s browsers supporting XHTML since they obviously don´t use an XML parser to first parse the CDATA node and then pass it on to the JavaScript interpreter.
I agree that having the SGML comments might become a problem once browsers start to behave according to the XML spec since then the whole script will be just a comment and will probably not get passed to the JavaScript interpreter.