encodeURIComponent () The definition and use of
encodeURIComponent () function can be the string encoded as a URI component.
Syntax
encodeURIComponent (URIstring)
Parameter Description
URIstring necessary. A string containing components or other URI-encoded text.
Return value
URIstring a copy of some of the characters will be the hexadecimal escape sequence to replace.
Description
This method will not ASCII encoding of letters and numbers, or punctuation these ASCII encoding: - _.! ~ * '().
Other characters (such as those used to separate :;/?:@&=+$,# components punctuation URI), by one or more hexadecimal escape sequence replacement.
Tips and Notes
Tip: Please note encodeURIComponent () function and encodeURI () function of the difference, which assumes that its parameter is the URI part (such as agreements, host name, path or query string). Therefore encodeURIComponent () function to escape the various parts of URI used to separate punctuation.
Examples
In this case, we will use encodeURIComponent () to encode URI:
<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
escape () method:
ISO Latin character set used to encode the specified string. All the space characters, punctuation, special characters, as well as other non-ASCII characters will be converted into% xx character encoding format (xx equal to the characters in the character set encoding table inside the 16-band digital). For example, at the corresponding code space is% 20. On the contrary unescape methods. This method will not be encoded characters: @ * / +
encodeURI () method:
The URI string using UTF-8 encoding format into a format string escape. This method will not be encoded characters:! @ # $ & * () =: /;? + '
encodeURIComponent () method:
The URI string using UTF-8 encoding format into a format string escape. And encodeURI () compared to this method will encode more characters, such as / etc characters. Therefore, if the URI string which contains several parts, then this method can not be used to encode, or / characters encoded URL will be displayed after the error. This method will not be encoded characters:! * ()
Black hair: http://heisetoufa.javaeye.com/