Summary of js encoding methods for strings (recommended), js Encoding

Source: Internet
Author: User

Summary of js encoding methods for strings (recommended), js Encoding

When using javascript to encode a URL string, although escape (), encodeURI (), and encodeURIComponent () can filter out special characters that affect URL integrity.

But the latter two is to convert the string to the UTF-8 to transfer, solve the problem of garbled code caused by different page encoding.

For example, if the send page is inconsistent with the Charset format of the receive page (assuming that the send page is GB2312 and the receive page is a UTF-8), use escape () the conversion and transmission of Chinese strings will cause garbled characters.

The following are various methods for URL encoding/Decoding in JS:

1. escape method: returns an encoded String object that can be read on all computers.

Function escape (charString: String): String

Characters not encoded by this method: @ */+

Note: The escape method returns a string value containing charstring content (in Unicode format ). All spaces, punctuation marks,

The accent and any other non-ASCII characters are replaced by % xx encoding, where xx equals to the hexadecimal number of the character.

For example, a space is returned as "% 20 ". (Characters with a character value greater than 255 are stored in % uxxxx format .)

Note: The escape method cannot be used to encode the Uniform Resource Identifier (URI. Use the encodeURI and encodeURIComponent methods for encoding.

2. encodeURI: returns a string encoded as a valid Uniform Resource Identifier (URI.

Function encodeURI (URIString: String): String

Characters not encoded by this method :! @ # $ & * () = :/;? +'

Description: The encodeURI method returns an encoded URI. If the encoding result is passed to decodeURI, the initial string is returned. EncodeURI does not encode the following characters: ":", "/", ";", and "?". Please use

EncodeURIComponent encodes these characters.

3. encodeURIComponent: return the string of a valid component encoded as a uniform resource identifier (URI.

Function encodeURIComponent (encodedURIString: String): String

Characters not encoded by this method :! *()'

Note: The encodeURIComponent method returns an encoded URI. If the encoding result is passed to decodeURIComponent, the initial string is returned. Because the encodeURIComponent method will encode all characters,

Note that if the string represents a path, for example,/folder1/folder2/default.html, the Slash will also be encoded, when this string is sent to the Web server as a request, it will be invalid. If the string contains multiple URI components, use the encodeURI Method for encoding.

4. unescape method: return the decoded String from the String object encoded using the escape method.

Function unescape (charString: String): String

Note: The unescape method returns a string value containing charstring content. All encoded in % xx hexadecimal format

The characters are replaced by equivalent characters in the ASCII character set. The characters encoded in % uxxxx format (Unicode character) are sixteen characters.

The Unicode Character of the hexadecimal encoded xxxx is replaced .)

Note: The unescape method should not be used to decode the Uniform Resource Identifier (URI ). Use the decodeURI and decodeURIComponent methods.

5. decodeURI method: return the non-encoding format of an encoded Uniform Resource Identifier (URI.

Function decodeURI (URIstring: String): String

DecodeURIComponent method: return the non-encoding form of an encoded component of the Uniform Resource Identifier (URI.

Function decodeURIComponent (encodedURIString: String): String

The URL encoding method in BTW: C...

Encoding: Server. UrlEncode (string)

Decoding: Server. UrlDecode (string) can be used to decode the first three client codes in the background.

The above is a summary (recommended) of all the js encoding methods provided by xiaobian for everyone. I hope you can provide a lot of support for the help house ~

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.