2

I follow the link How to convert javascript unicode notation code to utf-8? to run the function in my console.

function encode_utf8( s ){return unescape( encodeURIComponent( s ) );}( '\u4e0a\u6d77' )

Then I get:

"上海"

However, when I do this way:

foo = function(s){return unescape( encodeURIComponent( s ) );}
foo('\u4e0a\u6d77');
foo("\u4e0a\u6d77");

Then I get"ä¸æµ·" "ä¸æµ·"

What is wrong with the function? Thanks ahead.

EDIT: I thank you guys' explanation. Now I find that you just need to directly input in Chrome console '\u4e0a\u6d77', then I will get "上海". However my original problem is that I want to convert unicode code to utf-8 in the html file, not in console. I could not find the answer.

EDIT: Again, I want to thank you guys. Now I find that my problem is that I get string like '\\u4e0a\\u6d77' from txt file. (Note here there are two back slashes). How can I change it to '\u4e0a\u6d77' (I want to get rid of one back slash). Now I know once you get '\u4e0a\u6d77' (only one back slash) and then HTML will automatically show it as "上海"

EDIT: Now I find the solution: HERE

Community
  • 1
  • 1
kww
  • 31
  • 2
  • Try `(function encode_utf8( s ){return unescape( encodeURIComponent( s ) );}( '\u4e0a\u6d77' ))`… – Bergi May 07 '15 at 16:18

1 Answers1

2

Your first one is a function declaration followed by an unrelated expression in parentheses containing a string literals. The function is never called. The end result of that in the console is the value of the expression within the parens, which is the value of the string '\u4e0a\u6d77', which is of course "上海".

Your second one first creates the function (via a function expression), then calls it (twice, for some reason), passing in that string, and shows the function's return value.

So you see a difference because in the first case, you never call the function, you just get back the same string you provided. In the second case, you actually call the function and get back the UTF-8 data.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
  • Thanks. Now I see that I just need to directly input in Chrome console '\u4e0a\u6d77', then I will get "上海". How could I get "上海" in html from '\u4e0a\u6d77'? – kww May 07 '15 at 16:46
  • 1
    @kww: What do you mean "in HTML"? – T.J. Crowder May 07 '15 at 17:09
  • @kww: `'\u4e0a\u6d77' == "上海"`, so what? Just assign it to your html. If you want to use it in HTML markup that is not UTF8, use html entities. – Bergi May 07 '15 at 17:44