Need help with unterminated string constant ...

J

Jeff

Hi, does anyone know why this:

<a onclick="insertatcaret(window.opener.document.formname.fieldname,'<td
class="header">')">text</a>

returns a "Unterminated String Constant" error message in IE 6.0 but if
I take out the double quotes around header it does not? I need double
quotes around header, I thought this was possible in javascript so long
as the pair of double quotes is surrounded by a pair of single quotes.
 
J

Jeff

Thanks for the idea, but putting an escape character ( \ ) before each
quote around header does not help in this case.
 
M

Michael Winter

[fixed top-post]

Quote nesting only works once. That is

" ' ' "

is fine, but

" ' " " ' "

is not. The latter is interpreted as two separate quoted strings each
containing some spaces and a single apostrophe.

The same behaviour applies to both HTML and JavaScript.
Does the escape character "\" around your "header" help any?

It shouldn't. The mark-up is operating under HTML's parsing rules, not
JavaScript's. When the HTML parser encounters \", it sees a backslash
followed by a double quote. When the JavaScript parser encounters it, it
sees an escaped double quote. To solve it, do what you should always do
with restricted characters in HTML: use a character entity.

<a ... onclick="[...],'<td class=&quot;header&quot;>')">text</a>

Mike
 
M

Michael Winter

On Fri, 07 May 2004 00:05:33 GMT, Michael Winter

[snip]
To solve it, do what you should always do with restricted characters in
HTML: use a character entity.

I forgot: the angle brackets are also restricted. You should use character
entities for them too.
<a ... onclick="[...],'<td class=&quot;header&quot;>')">text</a>

....should be

<a ... onclick="[...],'&lt;td class=&quot;header&quot;&gt;')">

Mike
 
R

Richard Cornford

Don Sutter wrote:

Please do not top post on comp.lang.javascript. See the FAQ for details
of appropriate positing style for this group.
Does the escape character "\" around your "header" help any?

<a
onclick="insertatcaret(window.opener.document.formname.fieldname,'<td
class=\"header\">')">text</a>
<snip>

That will not help as the HTML parser is not aware of javascript escape
sequences and can still see the double quote as part of the page's
source code. It will take the first pair of quotes as delimiting the
string for the onclick attribute.

Instead a javascript hex escape sequence could be used in this context
as that would still result in the double quote in the resulting
javascript string but would be meaningless to the HTML parser:-

onclick="insertatcaret(
window.opener.document.formname.fieldname,
'<td class=\x22header\x22>'
);"

Richard.
 

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
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top