Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > javascript in xhtml files: avoid errors with characters > < & ' " ??

Reply
Thread Tools

javascript in xhtml files: avoid errors with characters > < & ' " ??

 
 
mark4asp
Guest
Posts: n/a
 
      01-08-2008
What is the best way to avoid errors with characters > < & " ' using
javascript in xhtml files?

Does it depent upon the type of xhtml?

So for each of xhtml 1.0 transitional, xhtml 1.0 strict, xhtml 1.1
strict. How should I tell the browser that the special xml characters
> < & " ' (for which xml mandates the use of entity values when they

appear as values) are to treated literally?

For instance is this (below) correct for all of xhtml 1.0 transitional,
xhtml 1.0 strict, xhtml 1.1 strict, or is it correct for only some of
them?

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

var x = (z>0 && z<10) ? "blah" : "blah blah"

// ]]>
</script>

When I use an external javascript file:
<script type="text/javascript" src="js/browser.js"></script>,
presumably I can dispense with the <![CDATA[ ... ]]> ?

PS: Sometimes I'm not able to use an external javascript file.
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      01-08-2008
mark4asp wrote:
> What is the best way to avoid errors with characters > < & " ' using
> javascript in xhtml files?


If you want to embed JavaScript code in an XHTML document then use a
CDATA section to wrap the script code e.g.
<script type="text/javascript"><![CDATA[
JavaScript code goes here
]]></script>

If you want to use XHTML but send it as text/html to browsers like IE
then you need to use a construct like you have below:

> <script type="text/javascript">
> // <![CDATA[
>
> var x = (z>0 && z<10) ? "blah" : "blah blah"
>
> // ]]>
> </script>


It does not depend on the version of XHTML at all as the need to use a
CDATA section results from XML rules which are the same for all XHTML
versions.

> When I use an external javascript file:
> <script type="text/javascript" src="js/browser.js"></script>,
> presumably I can dispense with the <![CDATA[ ... ]]> ?


Sure.


--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
David Dorward
Guest
Posts: n/a
 
      01-08-2008
On Jan 8, 11:41*am, "mark4asp" <(E-Mail Removed)> wrote:
> What is the best way to avoid errors with characters > < & " ' using
> javascript in xhtml files?
>
> Does it depent upon the type of xhtml?


Not really.

> So for each of xhtml 1.0 transitional, xhtml 1.0 strict, xhtml 1.1
> strict. *How should I tell the browser that the special xml characters> < & " ' (for which xml mandates the use of entity values when they
>
> appear as values) are to treated literally?


For XHTML 1.0 served as text/html, the spec says:

Use external style sheets if your style sheet uses < or & or ]]> or
--.

This may or may not be a requirement as the spec is badly written.
Postal's Law says to treat it as a requirement if you are authoring
XHTML.

> For instance is this (below) correct for all of xhtml 1.0 transitional,
> xhtml 1.0 strict, xhtml 1.1 strict, or is it correct for only some of
> them?
>
> <script type="text/javascript">
> // <![CDATA[
>
> var x = (z>0 && z<10) ? "blah" : "blah blah"
>
> // ]]>
> </script>


The JavaScript comments there are pointless and a waste of bytes, but
that is otherwise fine for any type of XHTML that is not served as
text/html.

You could also use &amp; et al, but that reduces readability.

> When I use an external javascript file:
> <script type="text/javascript" src="js/browser.js"></script>,
> presumably I can dispense with the <![CDATA[ ... ]]> ?


More than that, you must dispense with it. It is meaningless in JS.

--
David Dorward
http://dorward.me.uk/
http://blog.dorward.me.uk/
 
Reply With Quote
 
mark4asp
Guest
Posts: n/a
 
      01-08-2008
Martin Honnen wrote:

> mark4asp wrote:
> >What is the best way to avoid errors with characters > < & " ' using
> > javascript in xhtml files?

>
> If you want to embed JavaScript code in an XHTML document then use a
> CDATA section to wrap the script code e.g. <script
> type="text/javascript"><![CDATA[ JavaScript code goes here
> ]]></script>
>
> If you want to use XHTML but send it as text/html to browsers like IE
> then you need to use a construct like you have below:
>
> > <script type="text/javascript">
> > // <![CDATA[
> >
> > var x = (z>0 && z<10) ? "blah" : "blah blah"
> >
> > // ]]>
> > </script>

>
> It does not depend on the version of XHTML at all as the need to use
> a CDATA section results from XML rules which are the same for all
> XHTML versions.
>


Thanks for clarifying this. I interpret your reply to mean that I only
ever need

// <![CDATA[
...
// ]]>


for IE. At least 50% of my users are still using IE6.

I'm still a little bit puzzled that this distiction:
'send it as text/html to browsers like IE' vs. your first sentence.

Currently my files have a DOCTYPE specifying xhtml 1.0 transitional and
I have wrapped my javascript in
// <![CDATA[
...
// ]]>

99% of my users are using IE in a ie6:7 ratio of 60:40. All my file
extentions are either .aspx or .html

So is there any reason ever why I should NOT use the // comments
preceding the CDATA markers? Perhaps in a perfect world where all
users have up-to-date browers? The // is just there to stop the legacy
browsers seeing an error?


 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      01-08-2008
mark4asp wrote:

> Thanks for clarifying this. I interpret your reply to mean that I only
> ever need
>
> // <![CDATA[
> ...
> // ]]>
>
>
> for IE. At least 50% of my users are still using IE6.
>
> I'm still a little bit puzzled that this distiction:
> 'send it as text/html to browsers like IE' vs. your first sentence.


You need to use the JavaScript comment and CDATA wrapper as long as you
are delivering your XHTML documents as text/html as HTML parsers don't
understand XML CDATA sections. IE does not understand
application/xhtml+xml, the MIME type for XHTML documents, so most XHTML
contents is send as text/html, even for browsers like Mozilla or Opera
which understand application/xhtml+xml.

> 99% of my users are using IE in a ie6:7 ratio of 60:40. All my file
> extentions are either .aspx or .html


It does not depend on the file extension but rather on the HTTP
Content-Type header the server sends for a document. As long as your
..html document or your .aspx pages are served as text/html you need to
use the double wrapper of JavaScript comment and CDATA section.


--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
David Dorward
Guest
Posts: n/a
 
      01-08-2008
On Jan 8, 12:45*pm, "mark4asp" <(E-Mail Removed)> wrote:
> So is there any reason ever why I should NOT use the // comments
> preceding the CDATA markers? *Perhaps in a perfect world where all
> users have up-to-date browers? *The // is just there to stop the legacy
> browsers seeing an error?


Browsers are only going to see an error on CDATA markers if you claim
the XHTML is HTML (so why not use real HTML?), but the spec says that
if you do that then you should use an external file, so you wouldn't
be using CDATA markers at all.

--
David Dorward
http://dorward.me.uk/
http://blog.dorward.me.uk/

 
Reply With Quote
 
Jeremy J Starcher
Guest
Posts: n/a
 
      01-08-2008
On Tue, 08 Jan 2008 11:41:12 +0000, mark4asp wrote:

> What is the best way to avoid errors with characters > < & " ' using
> javascript in xhtml files?


The best solution might be to not use xhtml but use html 4.01 strict.

(IE 6 nor 7 recognize xhtml, rendering it as malformed html anyways and
will revert to operating in Quirks mode.)


 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      01-08-2008
On Jan 8, 10:20*am, Jeremy J Starcher <(E-Mail Removed)> wrote:
> On Tue, 08 Jan 2008 11:41:12 +0000, mark4asp wrote:
> > What is the best way to avoid errors with characters > < & " ' using
> > javascript in xhtml files?

>
> The best solution might be to not use xhtml but use html 4.01 strict.


Definitely.

>
> (IE 6 nor 7 recognize xhtml, rendering it as malformed html anyways and
> will revert to operating in Quirks mode.)


They revert to quirks mode only if the optional XML preamble is used.
This is because some brain-dead programmer at MS decided that the
doctype can only be found in the very first line of the markup.
 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      01-08-2008
On Jan 8, 2:41 pm, "mark4asp" <(E-Mail Removed)> wrote:
> What is the best way to avoid errors with characters > < & " ' using
> javascript in xhtml files?


Besides other helpful responses you may look at my post of 2006:

http://groups.google.com/group/comp....c6c90a43d9910f
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-08-2008
mark4asp wrote:
> Martin Honnen wrote:
>> [...]
>> If you want to use XHTML but send it as text/html to browsers like IE
>> then you need to use a construct like you have below:
>>
>>> <script type="text/javascript">
>>> // <![CDATA[
>>>
>>> var x = (z>0 && z<10) ? "blah" : "blah blah"
>>>
>>> // ]]>
>>> </script>

>> It does not depend on the version of XHTML at all as the need to use
>> a CDATA section results from XML rules which are the same for all
>> XHTML versions.

>
> Thanks for clarifying this. I interpret your reply to mean that I only
> ever need
>
> // <![CDATA[
> ...
> // ]]>
>
> for IE.


You misunderstood; "like IE" quite correctly means "IE and others". XHTML
is not yet universally supported on the Web. In fact, there are more user
agents that don't support it than user agents that support it, Gecko-based
UAs being one of the latter.

That said, those six bytes more won't do any harm but will do some good.

> At least 50% of my users are still using IE6.


How would you even know?

http://PointedEars.de/scripts/test/whatami

> I'm still a little bit puzzled that this distiction:
> 'send it as text/html to browsers like IE' vs. your first sentence.


http://www.hixie.ch/advocacy/xhtml
http://hsivonen.iki.fi/xhtml-the-point/

> Currently my files have a DOCTYPE specifying xhtml 1.0 transitional and
> I have wrapped my javascript in
> // <![CDATA[
> ...
> // ]]>
>
> 99% of my users are using IE in a ie6:7 ratio of 60:40.


Again, how would you even know? And more, what about the *future*? Even if
your numbers would be anywhere near correct, they don't allow a projection
about what will be tomorrow. Plus, interpreting those so-called
"statistics" that way creates a self-fulfilling prophecy: if you only code
for IE, you are unlikely to attract users that don't use IE, and you can
easily deceive yourself as being right in the first place when looking at
the resulting "statistics". (You may replace IE with any other UAs here.)

So, on the Web, you better hope for the best but are prepared for the worst
(however, IE suddenly vanishing would eventually be a Good Thing ).


PointedEars
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Should I Convert Site To XHTML or XHTML mobile? chronos3d HTML 9 12-05-2006 04:46 PM
Avoid having a SQL express for web parts and avoid personalization Roger23 ASP .Net 2 10-12-2006 10:54 PM
parse URL (href) from xhtml, xhtml -> text, for data hawat.thufir@gmail.com XML 7 02-08-2006 07:39 PM
Avoid wasting time or how to avoid initialization Alexander Malkis C++ 8 04-13-2004 11:23 PM
Errors, errors, errors Mark Goldin ASP .Net 2 01-17-2004 08:05 PM



Advertisments