Unicode Problem

G

geordino99

hello everybody,
I am getting some text from another website and the returned text is
returned in unicode like this "\\u0421\\u0442\\u0435\\u0444\\u0430\
\u043d \\u0425\\u0435"

I am trying to display this text in Javascript but it doesn't work

var txt => contains the returned text

alert(txt); // Displays texts correctly

someDiv.innerHTML = txt; //display the following:
\u0421\u0442\u0435\u0444\u0430\u043d \u0425\u0435

I tried the following but without success:
someDiv.innerHTML = eval( 'x="' + txt + '";');

if I define txt explicitly in javascript like this:
txt = "\\u0421\\u0442\\u0435\\u0444\\u0430\\u043d \\u0425\\u0435";
then it works

but when the txt is being loaded from the server using ajax it doesnt
work

I am using UTF-8 encoding in my html and ajax requests

any idea?
 
M

Martin Honnen

I am using UTF-8 encoding in my html and ajax requests

If you are using UTF-8, an Unicode encoding, why do you need to use \u
to escape characters at all? Simply transmit the characters, not their
escape sequences.
 
G

geordino99

If you are using UTF-8, an Unicode encoding, why do you need to use \u
to escape characters at all? Simply transmit the characters, not their
escape sequences.

thanks for the reply
I am not escaping the characters myself. I am getting this text from
external web service, the webservice returns the text like this.
 
M

Martin Honnen

I am not escaping the characters myself. I am getting this text from
external web service, the webservice returns the text like this.

Then you need to replace the escape sequences with the characters. Here
is an example:

var s = "\\u0421\\u0442\\u0435\\u0444\\u0430\\u043d \\u0425\\u0435";
s = s.replace(/\\u([0-9A-F]{4})/ig,
function (s, n) { return String.fromCharCode(parseInt(n, 16)); });


Now s is "Стефан Хе".
 
G

geordino99

I am not escaping the characters myself. I am getting this text from
external web service, the webservice returns the text like this.

Then you need to replace the escape sequences with the characters. Here
is an example:

var s = "\\u0421\\u0442\\u0435\\u0444\\u0430\\u043d \\u0425\\u0435";
s = s.replace(/\\u([0-9A-F]{4})/ig,
function (s, n) { return String.fromCharCode(parseInt(n, 16)); });

Now s is "Стефан Хе".

that's it! thank you very much :)
 
D

Dr J R Stockton

In comp.lang.javascript message <2215e769-9c12-4fbc-b701-bf9d456fea6e@b1
6g2000yqb.googlegroups.com>, Sun, 5 Apr 2009 03:26:10,
(e-mail address removed) posted:
I am getting some text from another website and the returned text is
returned in unicode like this "\\u0421\\u0442\\u0435\\u0444\\u0430\
\u043d \\u0425\\u0435"

Martin Honnen is undoubtedly right.

But I prefer to have my source code in ISO-7-UK, for which ASCII is an
almost exact substitute (some of my tools are DOS-based, without known
GUI equivalents); I'd change that to

"\u0421\u0442\u0435\u0444\u0430\u043d\u0425\u0435"

which is apparently not in my dictionary - a name?

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
T

Thomas 'PointedEars' Lahn

I am getting some text from another website and the returned text is
returned in unicode like this "\\u0421\\u0442\\u0435\\u0444\\u0430\
\u043d \\u0425\\u0435"

I am trying to display this text in Javascript but it doesn't work

var txt => contains the returned text

alert(txt); // Displays texts correctly

someDiv.innerHTML = txt; //display the following:
\u0421\u0442\u0435\u0444\u0430\u043d \u0425\u0435

Works as designed.
I tried the following but without success:
someDiv.innerHTML = eval( 'x="' + txt + '";');

This should work, however you don't need or want the assignment to an
undeclared identifier in the eval() code. And you better avoid
‘innerHTML’.

Please get a real name.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Martin Honnen said:
Then you need to replace the escape sequences with the characters

I presume it is more efficient to pass it to eval(). One has to add
string delimiters, of course.


PointedEars
 

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