Differences between encodeuri, escape, and encodeuricomponent in JS

Source: Internet
Author: User

JavaScript code involves three functions: Escape, encodeuri, and encodeuricomponent. The corresponding three decoding functions are: Unescape, decodeuri, and decodeuricomponent.

1,You must use encodeuricomponent when passing parameters so that the combined URL will not be truncated by special characters such.

For example:

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

2,Encodeuri can be used for URL redirection.

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

The urldecode () function can be used to encode characters in PHP.

3,You can use escape when JS uses data.

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

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

The following is an introduction to the three functions:

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:The escape method cannot be used to encode uniform resource identifiers (URIs. 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 for 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.