URL encoding conversion, escape () encodeURI () encodeURIComponent () _ basic knowledge

Source: Internet
Author: User
URL encoding conversion, escape () encodeURI () encodeURIComponent () Escape () method:
The specified string is encoded using the ISO Latin character set. All space characters, punctuation marks, special characters, and other non-ASCII characters will be converted into character encoding in % xx format (xx equals to the hexadecimal encoding of this character in the character set table number ). For example, the space character is encoded as % 20. The unescape method is the opposite. Characters not encoded by this method: @ */+

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."
Edge Core Javascript Guide: 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.


EncodeURI () method: converts a URI string into an escape string in UTF-8 encoding format. Characters not encoded by this method :! @ # $ & * () = :/;? +'

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. edge Core Javascript Guide: 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


EncodeURIComponent () method: convert a URI string to an escape string in UTF-8 encoding format. Compared with encodeURI (), this method will encode more characters, such. Therefore, if the string contains several parts of the URI, this method cannot be used for encoding. Otherwise, the URL will display an error after the/character is encoded. Characters not encoded by this method :! *()

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. mozilla Developer Core Javascript Guide: 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.

The Code is as follows:

Therefore, for a Chinese string, if you do not want to convert the string encoding format to the UTF-8 format (for example, when the charset of the original page and the target page is consistent), you only need to use escape. If your page is GB2312 or another code, and the page that accepts the parameter is UTF-8 code, use encodeURI or encodeURIComponent.

In addition, encodeURI/encodeURIComponent is introduced after javascript1.5, and escape is available in javascript1.0.

The escape () method does not encode the + character which is interpreted as a space on the server side as well as generated by forms with spaces in their fields. due to this variable coming, you shoshould avoid use of escape () whenever possible. the best alternative is usually encodeURIComponent (). use of the encodeURI () method is a bit more specialized than escape () in that it encodes for URIs [REF] as opposed to the querystring, which is part of a URL. use this method when you need to encode a string to be used for any resource that uses URIs and needs certain characters to remain un-encoded. note that this method does not encode the 'character, as it is a valid character within URIs. lastly, the encodeURIComponent () method shoshould be used in most cases when encoding a single component of a URI. this method will encode certain chars that wowould normally be recognized as special chars for URIs so that specified components may be encoded. note that this method does not encode the 'character, as it is a valid character within URIs.

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.