Unicode + Escape Character = IE Unterminated string constant error!

V

VUNETdotUS

Let's say I have a string:

div.innerHTML = "<a onclick='foo(\""+myWord+"\");'></a>";

in IE only (tested version 7) if var myWord = "English" then it works
fine
but if var myWord = "Modifier Cha\u00EEnes" then I get "Unterminated
string constant" error.

What fix would you suggest to keep div.innerHTML = "" format?
Thanks.
 
V

VUNETdotUS

Let's say I have a string:

div.innerHTML = "<a onclick='foo(\""+myWord+"\");'></a>";

in IE only (tested version 7) if var myWord = "English" then it works
fine
but if var myWord = "Modifier Cha\u00EEnes" then I get "Unterminated
string constant" error.

What fix would you suggest to keep div.innerHTML = "" format?
Thanks.

works well using escape() function. is it the best choice?..
 
P

pr

Define "works" - you have written an empty anchor in a document with an
onclick method that it is therefore impossible to launch. I'm guessing
your real code differs substantially from this example :)
works well using escape() function. is it the best choice?..

Can't reproduce the error message on IE6, and the message would normally
suggest a missing (or extra) quote mark rather than encoding trouble.
Perhaps the problem lies elsewhere in your code. Post the smallest
fragment of *working code* that displays the error. For example, this,
that gave me no problem:

function init() {
var div = document.getElementById("out");
var myWord = "Modifier Cha\u00EEnes";

div.innerHTML = "<a onclick='foo(\""+myWord+"\");'>" + myWord +
"</a>";
}

If you were encoding a URL you might use escape(), but not in the
onclick example you've supplied.
 
V

VUNETdotUS

Define "works" - you have written an empty anchor in a document with an
onclick method that it is therefore impossible to launch. I'm guessing
your real code differs substantially from this example :)



Can't reproduce the error message on IE6, and the message would normally
suggest a missing (or extra) quote mark rather than encoding trouble.
Perhaps the problem lies elsewhere in your code. Post the smallest
fragment of *working code* that displays the error. For example, this,
that gave me no problem:

function init() {
var div = document.getElementById("out");
var myWord = "Modifier Cha\u00EEnes";

div.innerHTML = "<a onclick='foo(\""+myWord+"\");'>" + myWord +
"</a>";
}

If you were encoding a URL you might use escape(), but not in the
onclick example you've supplied.

You are right. I had to simplify my code a lot. Instead of
div.innerHTML I had something like:

array.push("Something here ");
array.push("<a onclick='foo(\""+myWord+"\");'>" + myWord + "</a>");
array.join(' ');

Anyway, I used unescape() instead of escape() because my string looked
wrong. I believe this is not the perfect solution but is the fix for
now. I guessed I have to somehow isolate myWord with any function that
does not modify its value.
Thanks.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top