EncodeURI, encodeURIComponent, and escape are all used for encoding. This article will introduce the differences and Applications of encodeURI, encodeURIComponent, and escape. If you are interested, let's study together. First, we all know that these three items are used for encoding. First, encodeURI () and encodeURIComponent () are used for encoding and decoding during url conversion.
If there is encoding, there will be decoding. decoding is decodeURI () and decodeURIComponent (). Their usage is very simple. The purpose can be achieved by bringing the text to be transcoded into the parameters.
For example:
EncodeURI ("I am the text to be encoded ")
DecodeURI ("I am the text to be decoded ")
EncodeURIComponent ("I am the text to be encoded ")
DecodeURIComponent ("I am the text to be decoded ")
The difference between encodeURI () and encodeURIComponent () is not big.
The main difference is:
EncodeURI is not encoded with 82 characters :!, #, $, &, ', (,), *, +,-,.,/,:,;, = ,?, @,_,~, 0-9, a-z, A-Z
EncodeURIComponent has 71 unencoded characters :!, ',(,),*,-,.,_,~, 0-9, a-z, A-Z
EncodeURI is mainly used to assign values directly to the address bar:
Location. href = encodeURI ("http://www.cnblogs.com/Tezml ");
EncodeURIComponent is mainly used for url query parameters:
Location. href = "http://www.cnblogs.com/Tezml/test.php? A = "+ encodeURIComponent (" I am my ");
And escape is different from the two above.
Escape () is encoding, and unescape () is decoding;
Escape Method
Encode String objects so that they can be read on all computers,
Escape (charString)
The required charstring parameter is any String object or text to be encoded.
Description
The escape method returns a string containing the charstring content (in Unicode format ). All spaces, punctuation marks, accents, and other non-ASCII characters are replaced by % xx encoding,
Xx indicates the hexadecimal number of the character. For example, "% 20" is returned by a space ".
The characters with a value greater than 255 are stored in % uxxxx format.
Escape unencoded characters are 69: *, +,-,.,/, @, _, 0-9, a-z, A-Z
Note that the escape method cannot be used to encode the Uniform Resource Identifier (URI. Use the encodeURI and encodeURIComponent methods for encoding.
The last demo about encoding and decoding
Tezml _ encoding and decoding Test