Encodes the string as a URI component.
There are three functions in JavaScript that can encode strings, namely: Escape,encodeuri,encodeuricomponent, corresponding 3 decoding functions: Unescape,decodeuri, decodeURIComponent.
Escape () escapes all incoming strings in addition to ASCII letters, numbers, and specific symbols, so it is best not to use this method if you want to encode the URL.
encodeURI () encodes the entire URI, because the legitimate characters in the URI are not converted by encoding.
,/?:@&=+$#. Rightentire URLencoding, and the specific identifier of the URL is not transcoded.
The encodeURIComponent method, which encodes a single uricomponent (the request parameter), should be the most common, escaping the Chinese and special characters in the argument without affecting the entire URL.
<a href= "http://passport.baidu.com/?logout&aid=7&u= ' +encodeuricomponent (" http://cang.baidu.com/ Bruce42 ") + ' > Exit </a> ');</script>
The parameters in the URL are encoded because the parameter is also a URL, and if not encoded, it affects the entire URL of the jump.
The difference between the encodeURIComponent () function and the encodeURI () function.
encodeURIComponent () assumes that its arguments are part of a URI (such as a protocol, host name, path, or query string). Therefore, the encodeURIComponent () function escapes punctuation marks that are used to separate the various portions of the URI.
Return value: A copy of uristring, where some of the characters will be replaced by a hexadecimal escape sequence.
The method does not encode ASCII letters and numbers, nor does it encode these ASCII punctuation marks:-_. ! ~ * ' ().
Other characters (such as:;/?:@&=+$,# these are used toseparating punctuation from URI components), are made up of one or more hexadecimalEscape sequence SubstitutionOf
document.write (encodeURIComponent ("http://www.w3school.com.cn")) http%3a%2f%2fwww.w3school.com.cn
document.write (encodeURIComponent ("http://www.w3school.com.cn/P 1/")) http%3a%2f%2fwww.w3school.com.cn%2fp%201%2f
document.write (encodeURIComponent (",/?:@&=+$#"))%2c%2f%3f%3a%40%26%3d%2b%24%23
JavaScript encodeURIComponent ()