There are several functions that can encode strings in JavaScript, namely: escape, encodeURI, and encodeURIComponent.
1. escape
element
description
definition
The escape() function can encode a string so that it can be read on all computers
grammar
escape(string)
Parameter Description
The escape() function can encode a string so that it can be read on all computers
return value
A copy of the encoded string with some characters replaced by hexadecimal escape sequences
Description
This method does not encode ASCII letters and numbers, nor does it encode the following ASCII punctuation marks:-_.! ~ * '(). All other characters will be replaced by escape sequences.
decoding
unescape()
prompt
ECMAScript v3 opposes the use of this method, the application uses decodeURI() and decodeURIComponent() instead
The encodeURI() function can encode a string as a URI
grammar
encodeURI(URIstring)
Parameter Description
URIstring is required. A string containing URI or other text to be encoded.
return value
A copy of URIstring, some of which will be replaced by hexadecimal escape sequences
Description
This method does not encode ASCII letters and numbers, nor does it encode these ASCII punctuation marks:-_.! ~ * '(). The purpose of this method is to fully encode the URI, so the encodeURI() function will not escape the following ASCII punctuation that has a special meaning in the URI:; /?: @ & = + $, #.
decoding
decodes ()
prompt
If the URI component contains separators, such as? and #, you should use the encodeURIComponent() method to encode each component separately
The encodeURIComponent() function can encode a string as a URI component
grammar
encodeURIComponent(URIstring)
Parameter Description
URIstring is required. A string containing URI components or other text to be encoded
return value
A copy of URIstring, some of which will be replaced by hexadecimal escape sequences
Description
This method does not encode ASCII letters and numbers, nor does it encode these ASCII punctuation marks:-_.! ~ * '(). Other characters (for example:; /?: @ & = + $, # These punctuation marks used to separate URI components) are replaced by one or more hexadecimal escape sequences.
decoding
decodeURICompoent()
prompt
Note: The difference between encodeURIComponent() function and encodeURI() function, the former assumes that its parameters are part of the URI (such as protocol, hostname, path or query string). Therefore, the encodeURIComponent() function will escape the punctuation that separates the parts of the URI.
Summary: Through the analysis of the three functions, we know that: except for ASCII letters, numbers, and specific symbols, escape() encodes all incoming strings, so if you want to encode URLs, it’s best not to use them. This method. And encodeURI() is used to encode the entire URI, because legal characters in the URI will not be converted by encoding. The encodeURIComponent method should be the most commonly used when encoding a single URIComponent (referring to request parameters). It can escape the Chinese and special characters in the parameters without affecting the entire URL.