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