The Javascript encodeURI()
encodes a URI.
encodeURI(URI)
Parameters | Meaning |
---|---|
URI | A complete URI. |
The encodeURI()
function does not encode characters with special meaning for a URI.
encodeURI()
escapes all characters except:
A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #
The following code shows the difference between encodeURI()
and encodeURIComponent()
:
var set1 = ";,/?:@&=+$#"; // Reserved Characters var set2 = "-_.!~*'()"; // Unreserved Marks var set3 = "ABC abc 123"; // Alphanumeric Characters + Space console.log(encodeURI(set1));//w w w . j av a2 s. c o m console.log(encodeURI(set2)); console.log(encodeURI(set3)); console.log(encodeURIComponent(set1)); console.log(encodeURIComponent(set2)); console.log(encodeURIComponent(set3));
Javascript encodeURI()
can encode URIs.
It replaces all invalid characters with a special UTF-8 encoding.
The encodeURI()
method is designed to work on an entire URI.
encodeURI()
does not encode special characters that are part of a URI, such as the colon, forward slash, question mark, and pound sign.
let uri = "http:// www.java2s.com/illegal value.js#start"; console.log(encodeURI(uri));// w w w .ja v a 2s .c o m console.log(encodeURIComponent(uri));
The URI methods encodeURI()
, encodeURIComponent()
, decodeURI()
, and decodeURIComponent()
replace the escape()
and unescape()
methods, which are deprecated in the ECMA-262 third edition.