js-encoding function: Escape (), encodeURI (), encodeURIComponent ()

Source: Internet
Author: User

1. Escape ()

Escape () is the oldest of the JS coding functions. Although this function is now not advocated for use, but for historical reasons, many places still use it, it is necessary to start with it.

In fact, escape () cannot be used directly for URL encoding, and its true function is to return a Unicode encoded value of a character. For example, the "Spring Festival" return result is%u6625%u8282, that is, in the Unicode character set, "Spring" is the No. 6625 (hexadecimal) character, "section" is the No. 8282 (hexadecimal) character.

Escape ("\u6625\u8282"// Output "%u6625%u8282"  unescape ("%u6625%u8282"  // output "Spring Festival"

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

2, encodeURI ()

It encodes the entire URL, so in addition to the usual symbols, there are other symbols that have special meanings in the URL "; / ? : @ & = + $, # ", also not encoded. After encoding, it outputs the utf-8 form of the symbol and adds a% before each byte.

// = "http://www.cnblogs.com/season-huang/some%20other%20thing";

Its corresponding decoding function is decodeURI ().

3, encodeURIComponent ()

The last JavaScript encoding function is encodeURIComponent (). The difference from encodeURI () is that it is used to encode the part of the URL individually, not the entire URL.

Therefore, "; / ? : @ & = + $, # ", these symbols that are not encoded in encodeURI () are encoded in encodeURIComponent (). As for the specific coding method, the two are the same.

// = http%3a%2f%2fwww.cnblogs.com%2fseason-huang%2fsome%20other%20thing

Its corresponding decoding function is decodeURIComponent ().

Summarize:

1, if just encoded string, do not have a half-penny relationship with the URL, then use escape.

2, if you need to encode the entire URL, and then need to use this URL, then use encodeURI.

3, when you need to encode the parameters in the URL, then encodeURIComponent is the best way.

Report:

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

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

encodeURIComponent does not encode 71 characters:!, ', (,), *,-,.,_,~,0-9,a-z,a-z

js-encoding function: Escape (), encodeURI (), encodeURIComponent ()

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.