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 ()