encodeURIComponent () Definition and usage
The encodeURIComponent () function encodes a string as a URI component.
syntax :encodeuricomponent (uristring)
parameter Description : uristring required. A string that contains the URI component or other text to encode.
return value : A copy of 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.
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.
Instance:
In this example, we will encode the URI using encodeURIComponent ():
<script type= "Text/javascript" >document.write (encodeURIComponent ("http://www.w3school.com.cn")); document.write ("<br/>");d ocument.write (encodeURIComponent ("http://www.w3school.com.cn/p 1/")); document.write ("<br/>");d ocument.write (encodeURIComponent (",/?:@&=+$#"));</script>
Output:
Http%3a%2f%2fwww.w3school.com.cnhttp%3a%2f%2fwww.w3school.com.cn%2fp%201%2f%2c%2f%3f%3a%40%26%3d%2b%24%23
Escape () Method:
Encodes the specified string using the ISO Latin character set. All space characters, punctuation marks, special characters, and other non-ASCII characters are converted to the character encoding in the%xx format (XX equals the encoded 16-digit number of the character in the character set table). For example, the encoding for a space character is%20. The Unescape method is the opposite. Characters that are not encoded by this method: @ */+
encodeURI () Method:
Converts the URI string into an escape format using the UTF-8 encoding format. Characters that will not be encoded by this method:! @ # $& * () =:/;? +
encodeURIComponent () Method:
Converts the URI string into an escape format using the UTF-8 encoding format. Compared to encodeURI (), this method encodes more characters, such as characters. So if the string contains several parts of the URI, it cannot be encoded in this way, otherwise the URL will display an error after the/character is encoded. Characters that will not be encoded by this method:! * ( )
Therefore, for the Chinese string, if you do not want to convert the string encoding format into UTF-8 format (such as the original page and the target page charset is consistent), only need to use escape. If your page is GB2312 or other encoding, and the page that accepts the parameter is UTF-8 encoded, it is necessary to use encodeURI or encodeuricomponent.
In addition, Encodeuri/encodeuricomponent was introduced after javascript1.5, and escape is available in the javascript1.0 version.
JavaScript URL encoding conversion function encodeuricomponent ()