Recently do Android development, Android client Call Server page, but the server code for GBK, Android code for Utf-8, resulting in JS output error, the previous practice is to adjust the JS file encoding, but will generate two versions, very inconvenient, and finally find the form of transcoding to solve the Chinese characters. Which JS on the character of the function has encodeURI, encodeuricomponent, Escape. Let's take a brief look below.
1, encodeURI and decodeURI
[1] Syntax: encodeURI (String), decodeURI (String)
[2] Description: The decodeURI () function can decode the URI encoded by the encodeURI () function. Where the encryption value is: The hexadecimal escape sequence is replaced by the character they represent.
[3] Case:
<script type= "Text/javascript" >var test1= "Http://www.w3school.com.cn/My first/" document.write (encodeURI ( test1) + "<br/>") document.write (decodeURI (test1)) </script>
2, encodeURIComponent and decodeURIComponent
[1] Syntax: encodeURIComponent (String), decodeURIComponent (String)
[2] Description: The decodeURIComponent () function can decode the URI encoded by the encodeURIComponent () function. Where the encryption value is: The hexadecimal escape sequence is replaced by the character they represent.
[3] Case:
<script type= "Text/javascript" >var test1= "Http://www.w3school.com.cn/My first/" document.write (encodeURI ( test1) + "<br/>") document.write (decodeURI (test1)) </script>
3. Escape and Unescape
[1] Syntax: Escape (String), Unescape (String)
[2] Description: The unescape () function can decode a string encoded by escape ().
[3] Case:
<script type= "Text/javascript" >var test1= "Visit w3school!" Test1=escape (test1) document.write (test1 + "<br/>") test1=unescape (test1) document.write (test1 + "<br/>") </script>
Solve the problem of JS output Chinese characters garbled