JavaScript has three functions that can encode strings, namely escape, encodeURI, encodeURIComponent, then there will be three corresponding decoding functions: Unescape, decodeURI, decodeURIComponent.
Then the differences are as follows:
1 Escape () function
Definition and usage
The escape () function encodes the string so that it can be read on all computers.
Grammar
Escape (String)
Parameter description
string is required. The string to be escaped or encoded.
return value
A copy of the encoded string. Some of these characters are replaced with 16-binary escape sequences.
Description
The 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.
2 encodeURI () function
Definition and usage
The encodeURI () function encodes a string as a URI.
Grammar
encodeURI (uristring)
Parameter description
uristring required. A string that contains the URI or other text to encode.
return value
A copy of the uristring, where some of the characters will be replaced by a hexadecimal escape sequence.
Description
The 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 is not escaped for the following ASCII punctuation mark with a special meaning in the URI:;/?:@&=+$,#
3 encodeURIComponent () function
Definition and usage
The encodeURIComponent () function encodes a string as a URI component.
Grammar
encodeURIComponent (uristring)
Parameter description
uristring required. A string that contains the URI component or other text to encode.
return value
A copy of the uristring, where some of the characters will be replaced by a hexadecimal escape sequence.
Description
The method does not encode ASCII letters and numbers, nor does it encode these ASCII punctuation marks:-_. ! ~ * ' ().
Other characters (such as:;/?:@&=+$,# These punctuation marks used to separate the URI component) are replaced by one or more hexadecimal escape sequences.
Hints and Notes
Tip: Note the difference between the encodeURIComponent () function and the encodeURI () function, which assumes that its arguments are part of a URI (such as a protocol, hostname, path, or query string). Therefore, the encodeURIComponent () function escapes punctuation marks that are used to separate the various portions of the URI.
Summarize:
one, escape and they are not the same class
To put it simply, escape is to encode the string (string) (and the other two URLs) to make them readable on all computers.
The effect after encoding is%XX or%uxxxx this form.
Where ASCII alphanumeric @*/+ These characters will not be encoded, the rest will.
Most crucially, when you need to encode the URL, forget this method, which is used for strings and does not apply to URLs.
Second, the most commonly used encodeURI and encodeuricomponent
URL encoding is a common thing, so these two methods should be in practice to pay special attention to.
All of them are coded URLs,The only differenceis the coded character range, where the encodeURI methodwon'tEncode the following charactersASCII alphanumeric [email protected]#$&* () =:/,;? + '
encodeURIComponent methodwon'tEncode the following charactersASCII alphanumeric ~!* () '
So the encodeuricomponent is much larger than the encodeURI coding range.
As a practical example, encodeURIComponent will encode http://To HTTP%3A%2F%2F and encodeURI not.
Three, the most important, what kind of occasion should use what method
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.
Like what
encodeURI ("Http://www.cnblogs.com/season-huang/some other Thing");
Coded Result:
"Http://www.cnblogs.com/season-huang/some%20other%20thing";
Where the space is encoded as%20. But if you use encodeURIComponent, then the result becomes
"Http%3a%2f%2fwww.cnblogs.com%2fseason-huang%2fsome%20other%20thing"
See the difference, even the "/" is encoded, the entire URL has been unable to use.3, when you need to encode the parameters in the URL, then encodeURIComponent is the best way.
var param = "http://www.cnblogs.com/season-huang/"; param for parameter param = encodeuricomponent (param), var url = "http://www.cnblogs.com?next=" + param;console.log (URL)//"http ://www.cnblogs.com?next=http%3a%2f%2fwww.cnblogs.com%2fseason-huang%2f "
See the put, parameters in the "/" can be encoded, if the use of encodeURI must be a problem, because the next/is required to encode.
Escape (), encodeURI (), encodeuricomponent () differences