Does "<" need to be escaped?

T

Trev

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
 
T

Trev

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

Michael Winter

Trev said:
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.
 
C

cwdjrxyz

Trev said:
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.
 
C

cwdjrxyz

Trev said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top