encodeURI (uristring):
The method does not encode ASCII letters and numbers, nor does it encode these ASCII punctuation marks:-_. ! ~ * ' ().
The purpose of this method is to fully encode the URI, so the encodeURI () function is not escaped for the following ASCII punctuation mark with a special meaning in the URI:;/?:@&=+$,#
Tip: If the URI component contains delimiters, such as? and #, each component should be encoded using the encodeURIComponent () method.
In this example, we will encode the URI using 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.cnhttp://www.w3school.com.cn/my%20first/,/?:@&=+$#
encodeURIComponent (uristring):
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.
Tips:
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.
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/>") document.write (encodeURIComponent ("http://www.w3school.com.cn/p 1/")) document.write ("<br/>") document.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
encodeURIComponent () and encodeURI ()