Differences between escape, encodeuri, and encodeuricomponent

Source: Internet
Author: User

Escape (), encodeuri () and encodeuricomponent () are three common methods used to encode strings in Javascript, but their similarities and differences have plagued many JavaScript beginners, today, I will analyze and compare these three methods in detail.

Escape () method

In the msdn JScript reference:

The escape method returns a string value (in unicode format) that contains the contents of [the argument]. all spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with % XX encoding, where XX is equivalent to the hexadecimal number representing the character. for example, a space is returned as "% 20."

Simple translation: The escape method returns a string type value that contains the content of input parameters in unicode format. The escape method replaces all spaces, punctuation marks, accent characters, and any other non-ASCII characters in the input parameter with the % XX encoding format, the hexadecimal representation of XX and its characters is the same. For example, if the hexadecimal representation of the space character is 0x20, then XX should be 20, that is, escape ('') returns" % 20 ".

The Mozilla developer core JavaScript guide says:

The escape and Unescape functions let you encode and decode strings. the escape function returns the hexadecimal encoding of an argument in the ISO Latin character set. the Unescape function returns the ASCII string for the specified hexadecimal encoding value.

The escape and Unescape methods help you encode and decode strings. The escape method returns hexadecimal encoding for the character set of ISO Latin. Correspondingly, the Unescape method can convert hexadecimal encoding parameters into their ASCII format.

Encodeuri () method

In the msdn JScript reference:

The encodeuri method returns an encoded Uri. if you pass the result to decodeuri, the original string is returned. the encodeuri method does not encode the following characters: ":", "/", ";", and "? ". Use encodeuricomponent to encode these characters.

The encodeuri method returns an encoded Uri. If the encoding result of the encodeuri method is passed to the decodeuri method as a parameter, the original unencoded string is obtained. Note that the encodeuri method does not encode the following characters: ","/","; ", and "? ". To encode these characters, use the encodeuricomponent method.

The Mozilla developer core JavaScript guide says:

Encodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character.

By replacing each character set with one, two, or three (why one, two, or three, the escape sequence of the character is encoded by UTF-8 to encode a URI. For example ~! @ # $ % ^ & * () {} [] = :/,;? +/'"// Will be replaced ~! @ # $ % 25% 5E & * () % 7b % 7D % 5B % 5d = :/,;? + '% 22% 5C

Encodeuricomponent () method

In the msdn JScript reference:

The encodeuricomponent method returns an encoded Uri. if you pass the result to decodeuricomponent, the original string is returned. because the encodeuricomponent method encodes all characters, be careful if the string represents a path such as/folder1/folder2/default.html. the slash characters will be encoded and will not be valid if sent as a request to a Web server. use the encodeuri method if the string contains more than a single URI component.

The encodeuricomponent method returns an encoded Uri. If you pass the encoding result of the encodeuricomponent method to the encodeuricomponent method as a parameter, you can obtain the original unencoded string. Because the encodeuricomponent method encodes all characters, you must be careful when the string to be encoded is used to represent a path (such as/dir1/dir2/index.htm. The '/' symbol is no longer a valid path identifier after it is encoded, so it cannot be correctly recognized by the Web server. When a string contains a separate URI component? ), Use this method.

The Mozilla developer core JavaScript guide says:

Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character.

By replacing each character set with one, two, or three (why one, two, or three, to encode a uricomponent, we use UTF-8 encoding to represent the escape sequence of the character.

What is the difference? When to use?

From the above introduction, we can see that the MS documentation is obviously more detailed and easier to understand than Mozilla, but they all express a meaning. However, what are the similarities and differences between escape (), encodeuri (), and encodeuricomponent ()? Are they applicable to specific situations?

The escape method does not encode character +. We know that in the form field submitted by the user, if there is a space, it will be converted to a + character, and the server will think that the + number represents a space during parsing. Due to this defect, the escape method cannot correctly process all non-ASCII characters. Instead, you should try to avoid using the escape method. Instead, you 'd better choose the encodeuricomponent () method.

Unencoded characters of escape (): @ */+

Compared with the escape method, the encodeuri method is more professional. When you need to encode an entire Uri, you can use this method, because the valid characters in the URI are not encoded and converted. It should be noted that the character 'is also a valid character in the URI, so it will not be converted by encoding.

Encodeuri () unencoded characters :~! {Content} * () = :/,;? +'

The encodeuricomponent method is the most commonly used method for encoding a single uricomponent (request parameter. It should be noted that the character 'is also a valid character in the URI, so it will not be converted by encoding.

Encodeuricomponent () unencoded characters :~! *()'

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.