escape,encodeuri,encodeuricomponent function Comparison

Source: Internet
Author: User
Tags format comparison contains encode string functions resource valid
Comparison | function |escape|encodeuri|encodeuricomponent

JS encoding text involves 3 functions: Escape,encodeuri,encodeuricomponent, corresponding 3 decoding functions: Unescape,decodeuri,decodeuricomponent

1, the transfer of parameters need to use encodeuricomponent, so that the combination of the URL will not be # and other special characters truncated.

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> ');</script>

2, the URL jump can be used as a whole encodeURI

For example: Location.href=encodeuri ("http://cang.baidu.com/do/s?word= Baidu &ct=21");

3, JS use of data can use escape

For example: History records in the search of Tibet.

4, Escape to encode the Unicode value other than 0-255, output%u**** format, in other cases escape,encodeuri,encodeuricomponent encoding results are the same.

The most used should be encodeuricomponent, which is to convert Chinese, Korean and other special characters into utf-8 format URL encoding, So if you need to use encodeURIComponent when passing parameters to the background, you need background decoding for utf-8 support (the form is encoded in the same way as the current page encoding)

Escape does not encode 69 characters: *,+,-,.,/,@,_,0-9,a-z,a-z

There are 82 encodeURI not encoded characters:!,#,$,&, ', (,), *,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,a-z

There are 71 encodeuricomponent characters that are not encoded:!, ', (,), *,-,.,_,~,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 literal that you want to encode.

Description
The Escape method returns a string value (Unicode format) that contains the contents of the charstring. All spaces, punctuation, accented symbols, and other non-ASCII characters are replaced with%xx encodings, where xx equals the hexadecimal number that represents the character. For example, a space returns "%20".

Characters with a value greater than 255 are stored in%UXXXX format.

Note that the escape method cannot be used to encode a Uniform Resource Identifier (URI). The encodeURI and encodeURIComponent methods should be used 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 original string is returned. The encodeURI method does not encode the following characters: ":", "/", ";" and "?". Please encode these characters using the encodeURIComponent method.

encodeURIComponent method
Encodes a text string as 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 original string is returned. Because the encodeURIComponent method encodes all characters, note that if the string represents a path, such as/folder1/folder2/default.html, the slash will also be encoded. This will not be valid when the encoding result is sent as a request to the Web server. 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.