468,249 Members | 1,512 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,249 developers. It's quick & easy.

encoding of script tags in html

hi,

this is more an html parsing question than an XML question but I think it's
the kind of thing that folks in an XML newsgroup would be more likely to
help with, so please excuse me if it's a little off topic. please be aware
that I am primarily talking about HTML rather than XHTML but I would also
like to understand how XHTML works for when I prepare to convert the app to
XHTML.

I have recently discovered that this:

<script>var x='</script>';</script>

is not valid HTML - the fact that there is an end script tag in quotes
causes the parser to stop recognising the script. initially my reaction was
that this is not a surprise because I had failed to HTML encode the script
contents, so my second attempt was this:

<script>var x='&lt;/script&gt;';</script>

however this it DOES NOT WORK - the variable ends up containing the text
"&lt;/script&gt;"

can someone point me at part of the w3c specification that states how script
tags are parsed differently to other tags in HTML.

interestingly i have also discovered that this:

<script>if (3<5);</script>

IS valid html and seems even to be valid XHTML even though it is not valid
XML

Andy
Jun 27 '08 #1
4 3115
Andy Fish schrieb:
<script>var x='</script>';</script>
Escape the '/' in your script code:

var x='<\/script>';
--
Johannes Koch
In te domine speravi; non confundar in aeternum.
(Te Deum, 4th cent.)
Jun 27 '08 #2
Andy Fish wrote:
can someone point me at part of the w3c specification that states how script
tags are parsed differently to other tags in HTML.
See http://www.w3.org/TR/html4/types.html#type-script:
"Please note that script data that is element content may not contain
character references, but script data that is the value of an attribute
may contain them."
and http://www.w3.org/TR/html4/appendix/...ecifying-data:
"The DTD defines script and style data to be CDATA for both element
content and attribute values. SGML rules do not allow character
references in CDATA element content but do allow them in CDATA attribute
values."
interestingly i have also discovered that this:

<script>if (3<5);</script>

IS valid html and seems even to be valid XHTML even though it is not valid
XML
That snippet is not well-formed so it can't be valid XML or XHTML as it
is not even XML.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 27 '08 #3
thanks to both for the quick replies

wow - what a minefield this has turned out to be !!

i previously had just one server-side utility function to escape a string as
a literal javascript string. now i realise i need to have 2 separate
functions, one for when the javascript literal is to be placed inside an
HTML attribute value (e.g. onclick="...") and a different one for when it is
inside a script block, because one is CDATA and one is PCDATA

Andy
"Martin Honnen" <ma*******@yahoo.dewrote in message
news:48***********************@newsspool4.arcor-online.net...
Andy Fish wrote:
>can someone point me at part of the w3c specification that states how
script tags are parsed differently to other tags in HTML.

See http://www.w3.org/TR/html4/types.html#type-script:
"Please note that script data that is element content may not contain
character references, but script data that is the value of an attribute
may contain them."
and http://www.w3.org/TR/html4/appendix/...ecifying-data:
"The DTD defines script and style data to be CDATA for both element
content and attribute values. SGML rules do not allow character references
in CDATA element content but do allow them in CDATA attribute values."
>interestingly i have also discovered that this:

<script>if (3<5);</script>

IS valid html and seems even to be valid XHTML even though it is not
valid XML

That snippet is not well-formed so it can't be valid XML or XHTML as it is
not even XML.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jun 27 '08 #4
Martin Honnen wrote:
Andy Fish wrote:
>can someone point me at part of the w3c specification that states how
script tags are parsed differently to other tags in HTML.
[...]
>interestingly i have also discovered that this:

<script>if (3<5);</script>

IS valid html and seems even to be valid XHTML even though it is not
valid XML

That snippet is not well-formed so it can't be valid XML or XHTML as it
is not even XML.
It is, however, valid HTML (SGML): the < sign is valid unescaped in
CDATA declared content (and would be valid elsewhere, as the digit
following it cannot be taken to be the beginning of an element type
name).
wow - what a minefield this has turned out to be !!
Don't guess ("seems to be..."). Install a standalone validating parser
that handles both SGML and XML (eg onsgmls, part of SP), and copies of
the relevant DTDs; or a schema validator and copies of the schemas,
and test any files you create for validity. A good XML editor will do
this for you anyway.

///Peter
--
XML FAQ: http://xml.silmaril.ie/
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Mike Daniel | last post: by
12 posts views Thread by tshad | last post: by
1 post views Thread by Helicon | last post: by
8 posts views Thread by Erwin Moller | last post: by
7 posts views Thread by ojsimon | last post: by
5 posts views Thread by Andy Fish | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.