Javascript: escape, encodeURI, encodeURIComponent differences

Source: Internet
Author: User

The escape method does not encode character +. We know that in the form field submitted by the user, if there is a space, it will be converted to a + character, and the server will think that the + number represents a space during parsing. Due to this defect, the escape method cannot correctly process all non-ASCII characters. Instead, you should try to avoid using the escape method. Instead, you 'd better choose the encodeURIComponent () method.

Unencoded characters of escape (): @ */+

Compared with the escape method, the encodeURI method is more professional. When you need to encode an entire URI, you can use this method, because the valid characters in the URI are not encoded and converted. It should be noted that the character 'is also a valid character in the URI, so it will not be converted by encoding.

EncodeURI () unencoded characters :~! @ # $ & * () = :/,;? +'

The encodeURIComponent method is the most commonly used method for encoding a single URIComponent (request parameter. It should be noted that the character 'is also a valid character in the URI, so it will not be converted by encoding.

EncodeURIComponent () unencoded characters :~! *()'

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

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.