Escape, encodeuri, encodeuricomponent Function Comparison

Source: Internet
Author: User
Tags encode string
JavaScript code involves three functions: Escape, encodeuri, and encodeuricomponent. The corresponding three decoding functions are: Unescape, decodeuri, and decodeuricomponent.

1. encodeuricomponent is required when passing parameters so that the combined URL will not be truncated by special characters such.

Example: <script language = "JavaScript"> document. Write ('<a href = "http://passport.baidu.com /? Logout & Aid = 7 & U = '+ encodeuricomponent ("http://cang.baidu.com/bruce42") +' "> exit </a> '); </SCRIPT>

2. encodeuri can be used for URL redirection.

Example: location. href = encodeuri ("http://cang.baidu.com/do/s? WORD = Baidu & Ct = 21 ");

3. You can use escape when JS uses data.

For example, search for the history record in the bucket.

4. When encoding Unicode values other than 0-, escape outputs % u *** format. In other cases, escape, encodeuri, and encodeuricomponent have the same encoding result.

The most commonly used format is encodeuricomponent, which converts special characters such as Chinese and Korean into UTF-8 URL encoding, therefore, if you want to use encodeuricomponent to transmit parameters to the backend, you must use backend decoding to support UTF-8 (the encoding method in form is the same as that on the current page)

Escape unencoded characters are 69: *, +,-,.,/, @, _, 0-9, A-Z, A-Z

Encodeuri is not encoded with 82 characters :!, #, $, &, ', (,), *, +,-,.,/,:,;, = ,?, @,_,~, 0-9, A-Z, A-Z

Encodeuricomponent has 71 unencoded characters :!, ',(,),*,-,.,_,~, 0-9, A-Z, A-Z

Escape Method
Encode string objects so that they can be read on all computers,

Escape (charstring)

The required charstring parameter is any string object or text to be encoded.

Description
The escape method returns a string containing the charstring content (in unicode format ). All spaces, punctuation marks, accents, and other non-ASCII characters are replaced by % XX encoding. XX equals to the hexadecimal number of the character. For example, "% 20" is returned by a space ".

The characters with a value greater than 255 are stored in % uxxxx format.

Note that the escape method cannot be used to encode the Uniform Resource Identifier (URI. Use the encodeuri and encodeuricomponent methods for encoding.

Encodeuri Method
Encodes a text string into a valid Uniform Resource Identifier (URI ).

Encodeuri (uristring)

The required uristring parameter represents an encoded Uri.

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

Encodeuricomponent Method
Encodes a text string into a valid component of a uniform resource identifier (URI.

Encodeuricomponent (encodeduristring)

The required encodeduristring parameter represents an encoded URI component.

Description
The encodeuricomponent method returns an encoded Uri. If you pass the encoding result to decodeuricomponent, the initial string is returned. Because the encodeuricomponent method encodes all characters, note that if the string represents a path, for example,/folder1/folder2/default.html, the Slash will also be encoded. In this way, when the encoding result is sent to the Web server as a request, it will be invalid. If the string contains more than one URI component, use the encodeuri method.

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.