Comparison between JavaScript encodeuri and encodeuricomponent

Source: Internet
Author: User

Background
Both encodeuri and encodeuricomponent are functions defined in the ECMA-262 standard and will be implemented in all languages compatible with this standard (such as JavaScript and ActionScript. They are all global functions used to encode uri (RFC-2396) strings, but they are processed differently from the use case. To explain their differences, we first need to understand the character classification in the RFC-2396 for the URI
Reserved characters: these are reserved key characters in the URI, which are used to separate each part of the URI. These characters are: ";" | "/" | "? "|": "|" @ "|" & "|" = "|" + "|" $ "| ","
Mark character (Mark characters): This type of character is specifically defined in the RFC-2396, but is not specifically used, may be related to other RFC standards. These characters are: "-" | "_" | "." | "! "| "~ "|" * "|" '"|" ("| ")"
Alphanum characters: This type of character is the main part of a URI. It contains all uppercase letters, lowercase letters, and numbers.
After introducing the above three types of strings, we can easily explain the differences between the encodeuri and encodeuricomponent functions:
Encodeuri: This function is used to escape all non-(basic, Mark, and reserved) characters in the input string (escaping ). All characters to be escaped are converted to one, two, or three bytes of hexadecimal escape characters (% XX) According to UTF-8 encoding ). For example, convert the character space "" to "% 20 ". In this encoding mode, the ASCII characters to be encoded are replaced by one byte escape character, and the characters between \ u0080 and \ u007ff are replaced by two byte escape characters, the other 16 characters are Unicode characters, which are replaced by three-byte escape characters.
Encodeuricomponent: There is only one difference between the function processing method and encodeuri, that is, escape encoding for reserved characters. For example, the character ":" escaped character "% 3A" is replaced
The above two different functions are available because we are writing JS Code Uri. Encodeuri can be used to encode the complete URI string. Encodeuricomponent can encode a part of the URI so that this part can contain some reserved URI characters. This is very useful in our daily programming. For example, the following URI string:
Http://www.mysite.com/send-to-friend.aspx? Url = http://www.mysite.com/product.html
In this URI string. The send-to-friend.aspx page creates HTML-formatted mail content that contains a link whose address is the URL value in the URI string above. Obviously, the above URL value is a part of the URI, which contains the key characters reserved by the URI. We must call encodeuricomponent to encode the URI. Otherwise, the URI string above will be considered invalid by the browser. The correct URI should be as follows:
Http://www.mysite.com/send-to-friend.aspx? Url?http=3a%2f%2fwww.mysite.com%2fproduct.html
Example
Encodeuri Copy code The Code is as follows: var uri = "my test. asp? Name = sT å LE & Car = Saab ";
Document. Write (encodeuri (URI ));

The above output is as follows:
My % 20test. asp? Name = sT % C3 % a5le & Car = Saab

Encodeuricomponent Copy codeCode: var uri = "http://jb51.net/my test. asp? Name = sT å LE & Car = Saab ";
Document. Write (encodeuricomponent (URI ));

The above output is as follows:
HTTP % 3A % 2f % 2fjb51.net % 2fmy % 20test. asp % 3 fname % 3dst % C3 % a5le % 26car % 3 dsaab
Others
The decode global functions corresponding to the two encode functions are also defined in the ECMA-262 standard, which are decodeuri and decodeuricomponent. We can use them to decode the encoded strings.

Related Article

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.