Solve the problem of JS output Chinese characters garbled

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.