The difference between escape, encodeURI and encodeuricomponent _javascript skills

Source: Internet
Author: User

Escape () method

The MSDN JScript reference says:

The Escape method returns a string value, in Unicode format, that contains the contents of [the argument]. All spaces, punctuation, accented characters, and no other non-ascii characters-are with replaced%xx, where xx is equivalent to the hexadecimal number representing the character. For example, a and returned as "%20."

The Escape method returns a string containing the contents of an incoming argument in Unicode format. The escape method replaces all spaces, punctuation marks, accented characters, and any other non-ASCII characters in the incoming argument with the encoded form of%XX, where xx is the same as the 16-digit representation of the character it represents. If the 16 binary representation of a space character is 0x20, then xx should be 20, that is, Escape (') return to "%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 the argument in the ISO Latin character set. The Unescape function returns the ASCII string for the specified hexadecimal encoding value.

Escape and Unescape methods can help you encode and decode strings. The Escape method returns its 16-encoded encoding for a parameter consisting of characters in the ISO Latin character set. Correspondingly, the Unescape method converts the parameters of the 16-encoded form into its ASCII code form.

encodeURI () method

The MSDN JScript reference says:

The encodeURI method returns a encoded URI. If you are the result of the 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 an argument, the original unsigned string can be obtained. It should be noted that the encodeURI method does not encode the following characters ":", "/", ";", and "?". If you want to encode these characters, use the encodeURIComponent method.

The Mozilla Developer Core Javascript guide says:

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

By replacing each character that belongs to a particular set of characters with one, two, or three (why is "one, two, or three"), I don't know, I'm looking for an escape sequence to encode a URI by using UTF-8 encoding to represent this character. such as ~!@#$%^&* () {}[]=:/,;? +\ ' "\ will be replaced by ~!@#$%25%5e&* ()%7b%7d%5b%5d=:/,;? + '%22%5c

encodeURIComponent () method

The MSDN JScript reference says:

The encodeURIComponent method returns a encoded URI. If you are the result of the 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 would be encoded and won't 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 the encoding result of the encodeURIComponent method is passed to the encodeURIComponent method as an argument, the original unsigned string can be obtained. Because the encodeURIComponent method encodes all the characters, use caution if the string being encoded is used to represent a path, such as/dir1/dir2/index.htm. The '/' symbol will be encoded and will no longer be a valid path identifier, so it cannot be properly identified by the Web server. Use this method when the string contains a separate URI component (referring to the request parameters later).

The Mozilla Developer Core Javascript guide says:

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

By replacing each character that belongs to a particular character set with one, two, or three (why is "one, two, or three" I don't understand, hope expert enlighten) Use UTF-8 encoding to represent the escape sequence of this character to encode a uricomponent.

What's the difference? When to use?

As you can see from the above, MS documents are clearly more detailed and understandable than Mozilla, but they are all meant to be an expression. But what are the similarities and differences between escape (), encodeURI () and encodeuricomponent () that apply to that particular situation?

The escape method does not encode characters +. And we know that in the user submitted form field, if there is a space, it will be converted to the + character, and the server resolution will be considered as the + number represents a space. Because of this flaw, the escape method does not handle all non-ASCII characters correctly, and you should try to avoid using the escape method instead, and you'd better choose the encodeURIComponent () method.

Escape () characters not encoded: @*/+

Using the encodeURI method is more professional than using the escape method. When you need to encode an entire URI, you can use this method, because the legal characters in the URI are not converted by encoding. Note that the character ' is also the legal character in the URI, so it will not be converted by encoding.

encodeURI () characters not encoded: ~!@#@{content}* () =:/,;? +'

The encodeURIComponent method should be most commonly used in encoding a single uricomponent (referring to request parameters). Note that the character ' is also the legal character in the URI, so it will not be converted by encoding.

encodeURIComponent () characters not encoded: ~!* () '

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.