Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #34794
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: The less sign "<" in script elements |
| Date | 2018-03-19 00:41 +0100 |
| Organization | PointedEars Software (PES) |
| Message-ID | <2015900.iZASKD2KPV@PointedEars.de> (permalink) |
| References | <less-20180318165920@ram.dialup.fu-berlin.de> |
Stefan Ram wrote:
> So, is my memory correct that in HTML 2, HTML 3 and/or
> HTML 4, one had to escape a less sign in a script element,
> using, e.g., »<«? E.g., for a comparison like »x<y«?
No. Escaping using *other* means was only necessary if “<” was followed by
“/”.
The SCRIPT element was formally introduced in HTML 3.2 (there is no HTML
3.0). However, in HTML 3.2 to 4.01, because of limitations of the SGML-
based DTD, the CDATA content of a SCRIPT element was defined to end at the
first occurrence of “</” (End-Tag Open delimiter, ETAGO). And this
substring occurred frequently in script code that generated HTML (especially
before the W3C DOM, hence the example below).
The easiest workaround, thus recommended and used by me as well, was to
write "<\/" in a string literal, or to move the source code for generated
markup to an external script resource. (Later, you could also get rid of
“</” by strictly using features introduced with the W3C DOM.)
[The misconception that it would be necessary to hide the offending
content from *seemingly* bad validators and editors (which were
actually *working*) caused the misconception in many people that in
XHTML (and in HTML for *actually* bad validators and editors) it would
a good idea to comment out the entire script contents:
<script …><!-- … --></script>
(a common example of this was code generated by Macromedia Dreamweaver)
But that merely masks the markup errors which is a problem when serving
XHTML with the proper Content-Type “application/xhtml+xml” (those
people then resorted to serving XHTML with Content-Type “text/html”,
which I think contributed to the demise of XHTML – most people simply
did not bother to understand the powerful tool that they were given).
The correct way in XHTML (as “application/xhtml+xml”) is, of course,
as you indicated,
<script …><![CDATA[ … ]]></script>
because the CDATA section declaration makes clear where the CDATA
begins and ends. However “]]>” must to be escaped instead, or an
external script resource must be used.]
I have written (this) *countless* times (about that) in several
newsgroups/forums, including this one; so have other people [the first
Google hit for “script etago” is an article by Mathias Bynens citing a test
case by IIRC then-cljs regular Juriy ‘kangax’ Zaytsev (Юрий Зайцев)].
You can still verify it if you use the W3C Markup Validator at
<http://validator.w3.org/>:
1. <https://validator.w3.org/#validate_by_input+with_options>
2. More Options → (·) Validate HTML fragment → Use Doctype: HTML 4.01
3. Markup to validate (without indentation):
<script type="text/javascript">document.write("<p>foo</p>");</script>
4. Check →
5. “Error: end tag for element "P" which is not open
[…]
If this error occurred in a script section of your document, you
should probably read this FAQ entry
<https://validator.w3.org/docs/help.html#faq-javascript>.”
--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next | Find similar | Unroll thread
Re: The less sign "<" in script elements Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-03-19 00:41 +0100
csiph-web