Unicode Problem

Discussion in 'Javascript' started by geordino99@googlemail.com, Apr 5, 2009.

  1. Guest

    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?
    , Apr 5, 2009
    #1
    1. Advertising

  2. wrote:

    > 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.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Apr 5, 2009
    #2
    1. Advertising

  3. Guest

    On Apr 5, 3:34 am, Martin Honnen <> wrote:
    > wrote:
    > > 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.
    >
    > --
    >
    >         Martin Honnen
    >        http://JavaScript.FAQTs.com/


    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.
    , Apr 5, 2009
    #3
  4. wrote:

    > 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 "Стефан Хе".

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Apr 5, 2009
    #4
  5. Guest

    On Apr 5, 4:35 am, Martin Honnen <> wrote:
    > wrote:
    > > 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 "Стефан Хе".
    >
    > --
    >
    >         Martin Honnen
    >        http://JavaScript.FAQTs.com/


    that's it! thank you very much :)
    , Apr 5, 2009
    #5
  6. In comp.lang.javascript message <2215e769-9c12-4fbc-b701-bf9d456fea6e@b1
    6g2000yqb.googlegroups.com>, Sun, 5 Apr 2009 03:26:10,
    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.

    --
    (c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF3 Op9 Sf3
    news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr J R Stockton, Apr 5, 2009
    #6
  7. <> wrote:
    > 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
    Thomas 'PointedEars' Lahn, Apr 5, 2009
    #7
  8. Martin Honnen <> wrote:
    > wrote:
    >
    > > 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


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


    PointedEars
    Thomas 'PointedEars' Lahn, Apr 5, 2009
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Mark Bram
    Replies:
    0
    Views:
    3,920
    Robert Mark Bram
    Sep 28, 2003
  2. ygao

    unicode wrap unicode object?

    ygao, Apr 8, 2006, in forum: Python
    Replies:
    6
    Views:
    547
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Apr 8, 2006
  3. Gabriele *darkbard* Farina

    Unicode digit to unicode string

    Gabriele *darkbard* Farina, May 16, 2006, in forum: Python
    Replies:
    2
    Views:
    517
    Gabriele *darkbard* Farina
    May 16, 2006
  4. gabor
    Replies:
    13
    Views:
    550
    Leo Kislov
    Nov 18, 2006
  5. Jean-Paul Calderone
    Replies:
    23
    Views:
    671
    Leo Kislov
    Nov 21, 2006
Loading...

Share This Page