Forum Moderators: open

Message Too Old, No Replies

xhtml and script tags

but what about &, <, > is that valid?

         

joshie76

3:39 pm on Sep 12, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A question I posed in another thread on xslt [webmasterworld.com] has just led to me trying an experiment which totally shocked me (I haven't yet done any xhtml work).

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?

dingman

4:36 pm on Sep 12, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I'm not mistaken, what you have to do is to use and XHTML CDATA section to surround your <script> tag. I haven't actually tried this, however. What I have done is to put my javascript into an external file and use a tag like the following:

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

andreasfriedrich

4:53 pm on Sep 12, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dingman is dead right. As is quite often the case reading the documents provided by the W3C will provide the answer [w3.org].

joshie76

8:08 pm on Sep 12, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You mean like this?

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

c3oc3o

8:27 pm on Sep 12, 2002 (gmt 0)

10+ Year Member



<script type="text/javascript"> <![CDATA[ if (h && i) j(); ]]> </script>
The problem with this solution is that not many browsers understand this synatx either. You might try wrapping the CDATA markers inside comments. (Use the comments of your scripting or style-sheet language, mind you. If you use the SGML-style comments, all sorts of nastiness may ensue.)

from: [mit.edu...]

Which, as I read it, means that this is recommended:
<script type="text/javascript">
//<![CDATA[
if (h && i) j();
//]]>
</script>

andreasfriedrich

8:36 pm on Sep 12, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<script type="text/javascript"><!-- //<![CDATA[ 
alert('hello');
//]]>--></script>

combines both suggested versions.

joshie76

8:42 pm on Sep 12, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks c3oc3o,

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 ;)?

joshie76

8:22 am on Sep 13, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Andreas, 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.

joshie76

8:34 am on Sep 13, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That really is a very interesting article c3o,

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?

andreasfriedrich

11:31 am on Sep 13, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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.

joshie76

11:55 am on Sep 13, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



But since the CDATA node is also ignored there's no point including it, no matter which way you look at it;).

Unless of course you use it without <!-- --> as shown in c3o's post.