469,366 Members | 1,805 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Does "<" need to be escaped?

Hi everyone,
Thanks to all who have helped with various issues in the past. I've
come up with a new one though:
I've run some html through a javascript converter; basically it takes
the html and outputs the
following:

var str='';
str+= // whatever text I want
document.write(str);

However, this has converted the < character in html to <\
I didn't know it needed to be escaped! All the double quotes in the
converted string aren't escaped- is this because the string is encased
in single, not double quotes? The single quotes inside the string are
escaped though.

TIA

Trev

Oct 27 '06 #1
4 1379
Dammit! Looked out at the converted code, and its not the "<" thats
being escaped - it's the forward slash, so I get \/ all over the place!
I thought only backslashes needed escaping?

Oct 27 '06 #2
Trev wrote:
Dammit! Looked out at the converted code, and its not the "<" thats
being escaped - it's the forward slash, so I get \/ all over the
place! I thought only backslashes needed escaping?
In script and style element content, the sequence "</" (ETAGO) followed
by a NAME character (letters, primarily) can be interpreted as the end
of that element. This is necessary because some elements have optional
end-tags, so anything that looks like one can be treated that way[1]. To
prevent that, the sequence needs to be broken, and the easiest means to
that end is to escape the slash.

This isn't necessary for external scripts, though it does no real harm,
either.

Mike
[1] If the actual end-tag turned out not to be for a script or
style element, the document would be invalid.
Oct 27 '06 #3

Trev wrote:
Dammit! Looked out at the converted code, and its not the "<" thats
being escaped - it's the forward slash, so I get \/ all over the place!
I thought only backslashes needed escaping?
If I understand you correctly, you will find a discussion of the need
for backslashing in certain document.write situations at
http://www.htmlhelp.com/tools/validator/problems.html near the top of
the page concerning script. This page also discusses several other
things that often prompt questions that arise from a validation error
report.

Oct 27 '06 #4

Trev wrote:
Dammit! Looked out at the converted code, and its not the "<" thats
being escaped - it's the forward slash, so I get \/ all over the place!
I thought only backslashes needed escaping?
If I understand you correctly, you will find a discussion of the need
for backslashing in certain document.write situations at
http://www.htmlhelp.com/tools/validator/problems.html near the top of
the page concerning script. This page also discusses several other
things that often prompt questions that arise from a validation error
report.

Oct 27 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Eric A. Forgy | last post: by
1 post views Thread by Christian Schmidbauer | last post: by
7 posts views Thread by Diandian Zhang | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.