Encodeuri,encodeuricomponent encoding

Source: Internet
Author: User
Tags decode all alphanumeric characters

encodeURI (), encodeURIComponent (), decodeURI (), decodeuricomponent () URL encoding

The encodeURI () and encodeURIComponent () methods of the global object encode the URI for sending to the browser. A valid URI cannot contain certain characters, such as an empty
Pane These two URI encoding methods can encode URIs, which replace all invalid characters with special UTF-8 encoding, allowing the browser to accept and understand.

where encodeURI () is used primarily for the entire URI (for example, Http://www.w3cmm.com/illegal value.htm), and encodeURIComponent () Primarily used to encode a segment in a URI, such as illegal value.htm in the preceding URI. The main difference is that encodeURI () does not encode special characters of its own term URI, such as colons, forward slashes, question marks, and well sizes, while encodeuricomponent () encodes any non-standard characters it discovers. Take a look at the following example:

var uri = "Http://www.w3cmm.com/illegal Value.htm#start";
"Http://www.w3cmm.com/illegal%20value.htm#start"
Alert (encodeURI (URI));

"Http%3a%2f%2fwww.w3cmm.com%2fillegal%20value.htm%23start"
Alert (encodeURIComponent (URI));

The result of using encodeURI () encoding is that characters other than spaces are left intact, and only spaces are replaced with%20. The encodeURIComponent () method replaces all non-alphanumeric characters with the corresponding encoding. This is also the reason why you can use encodeURI () for the entire URI and only use encodeURIComponent () for strings appended to the existing URI.

In general, we use the encodeURIComponent () method more often than with encodeURI (), because in practice it is more common to encode query string parameters rather than base URIs.

The two methods corresponding to the encodeURI () and encodeURIComponent () methods are decodeURI () and decodeURIComponent () respectively. where decodeURI () can only decode characters that are replaced with encodeURI (). For example, it can replace%20 with a space, but does not do anything with%23 because%23 represents the well font size (#), and the well font size is not replaced with encodeURI. Similarly, decodeuricomponent () can decode all characters encoded using encodeuricomponent (), that is, it can decode the encoding of any special character. Take a look at the following example:

var uri = "Http%3a%2f%2fwww.w3cmm.com%2fillegal%20value.htm%23start"
"Http%3a%2f%2fwww.w3cmm.com%2fillegal Value.htm%23start"
Alert (decodeURI (URI));

"Http://www.w3cmm.com/illegal Value.htm#start"
Alert (decodeURIComponent (URI));

Here, the variable URI contains a string encoded by encodeURIComponent (). In the result of the first call to the decodeURI () output, only%20 is replaced with a space. In the result of the second call to the decodeURIComponent () output, the encoding of all special characters is replaced with the original character, and an escaped string is obtained.

The URI Method encodeURI (), encodeURIComponent (), decodeURI (), and decodeuricomponent () are used to replace the escape () and ECMA-262 () that have been discarded by the third version of the unescape () Method. The URI method can encode all Unicode characters, whereas the original method can encode ASCII characters correctly. Therefore, in the development practice, especially in the product-level code, be sure to use the URI method, do not use the escape () and the Unescape () method.

Encodeuri,encodeuricomponent encoding

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.