There are three functions in JavaScript that can encode strings: escape, encodeURI, encodeURIComponent,
Corresponding three decoding functions: unescape, decodeURI, and decodeURIComponent.
The following is a brief introduction of their differences.
1 escape () function
Definition and usage
The escape () function can encode a string so that the string can be read on all computers.
Syntax
Escape (string)
Parameter description
String is required. The string to be escaped or encoded.
Return Value
A copy of the encoded string. Some characters are replaced with hexadecimal escape sequences.
Description
This method does not encode ASCII letters and numbers or the following ASCII punctuation marks :-_.! ~ *'(). All other characters are replaced by escape sequences.
------------------------------------------------------------------------
2 encodeURI () function
Definition and usage
The encodeURI () function can encode a string as a URI.
Syntax
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 the hexadecimal escape sequence.
Description
This method does not encode ASCII letters and numbers, and does not encode these ASCII punctuation marks :-_.! ~ *'().
The purpose of this method is to fully encode the URI. Therefore, the encodeURI () function will not escape the following ASCII punctuation marks with special meanings in the URI :;/? : @ & =+ $ ,#
------------------------------------------------------------------------
3 encodeURIComponent () function
Definition and usage
The encodeURIComponent () function can encode a string as a URI component.
Syntax
EncodeURIComponent (URIstring)
Parameter description
URIstring is required. A string containing the URI component or other text to be encoded.
Return Value
A copy of URIstring, some of which will be replaced by the hexadecimal escape sequence.
Description
This method does not encode ASCII letters and numbers, and does not encode these ASCII punctuation marks :-_.! ~ *'().
Other characters (such :;/? : @ & =+ $, # The punctuation marks used to separate URI components) are all replaced by one or more hexadecimal escape sequences.
Tips and comments
Tip: note the differences between the encodeURIComponent () function and the encodeURI () function. The former assumes that its parameters are part of the URI (such as the protocol, host name, path, or query string ). Therefore, the encodeURIComponent () function uses escape characters to separate the punctuation marks of each part of the URI.
------------------------------------------------------------------------
4. Conclusion:
By analyzing the three functions, we can know that in addition to ASCII letters, numbers, and specific symbols, escape () can escape and encode all the passed strings, therefore, if you want to encode the URL, it is best not to use this method. EncodeURI () is used to encode the entire URI, because the valid characters in the URI are not encoded and converted. The encodeURIComponent method is the most commonly used method for encoding a single URIComponent (request parameter). It can escape Chinese and special characters in parameters without affecting the whole URL.
5 examples:
1. escape ()
<Script type = "text/javascript">
Document. write (escape ("http://www.w3school.com.cn") + "<br/> ")
Document. write (escape ("?! = () # % &"))
</Script> output:
<Type = "text/javascript">
Http % 3A // www.w3school.com.cn
% 3F % 21% 3D % 28% 29% 23% 26
2. encodeURI ()
<Script type = "text/javascript">
Document. write (encodeURI ("http://www.w3school.com.cn") + "<br/> ")
Document. write (encodeURI ("http://www.w3school.com.cn/My first /"))
Document. write (encodeURI (",/? : @ & = + $ #"))
</Script> output:
Http://www.w3school.com.cn
Http://www.w3school.com.cn/My%20first/
,/? : @ & =+ $ #
Encode the entire URL, and the specific Identifier of the URL will not be transcoded.
3. encodeURIComponent ()
Example 1:
<Script type = "text/javascript">
Document. write (encodeURIComponent ("http://www.w3school.com.cn "))
Document. write ("<br/> ")
Document. write (encodeURIComponent ("http://www.w3school.com.cn/p 1 /"))
Document. write ("<br/> ")
Document. write (encodeURIComponent (",/? : @ & = + $ #"))
</Script>
Output:
Http % 3A % 2F % 2Fwww.w3school.com.cn
Http % 3A % 2F % 2Fwww.w3school.com.cn % 2Fp % 201% 2F
% 2C % 2F % 3F % 3A % 40% 26% 3D % 2B % 24% 23
Example 2: <script language = "javascript"> document. write ('<a href = "http://passport.baidu.com /? Logout & aid = 7 & u = '+ encodeURIComponent ("http://cang.baidu.com/bruce42") +' "> exit </a> ');
</Script>
Encode the parameters in the URL, because the parameter is also a URL. If it is not encoded, the jump of the whole URL will be affected.