Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

javascript - 用Javascript解码UTF-8(Decode UTF-8 with Javascript)

I have Javascript in an XHTML web page that is passing UTF-8 encoded strings.(我在传递UTF-8编码字符串的XHTML网页中有Javascript。)

It needs to continue to pass the UTF-8 version, as well as decode it.(它需要继续传递UTF-8版本,并对其进行解码。) How is it possible to decode a UTF-8 string for display?(如何解码UTF-8字符串以进行显示?) <script type="text/javascript"> // <![CDATA[ function updateUser(usernameSent){ var usernameReceived = usernameSent; // Current value: Gr????????e var usernameDecoded = usernameReceived; // Decode to: Gr??e var html2id = ''; html2id += 'Encoded: ' + usernameReceived + '<br />Decoded: ' + usernameDecoded; document.getElementById('userId').innerHTML = html2id; } // ]]> </script>   ask by Jarrett Mattson translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To answer the original question: here is how you decode utf-8 in javascript:(要回答原始问题:这是如何在javascript中解码utf-8:)

http://ecmanaut.blogspot.ca/2006/07/encoding-decoding-utf8-in-javascript.html(http://ecmanaut.blogspot.ca/2006/07/encoding-decoding-utf8-in-javascript.html) Specifically,(特别,) function encode_utf8(s) { return unescape(encodeURIComponent(s)); } function decode_utf8(s) { return decodeURIComponent(escape(s)); } We have been using this in our production code for 6 years, and it has worked flawlessly.(我们已经在生产代码中使用了6年,并且它运行良好。) Note, however, that escape() and unescape() are deprecated.(但是请注意,不建议使用escape()和unescape()。) See this.(看到这个。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...