encodeURI() function encodes a URI by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two surrogate characters). Compared to
encodeURIComponent(), this function encodes fewer characters, preserving those that are part of the URI syntax.
- : A string to be encoded as a URI.
A new string representing the provided string encoded as a URI.
encodeURI() is a function property of the global object.
encodeURI() function escapes characters by UTF-8 code units, with each octet encoded in the format
%XX, left-padded with 0 if necessary. Because lone surrogates in UTF-16 do not encode any valid Unicode character, they cause
encodeURI() to throw a
encodeURI() escapes all characters except:
A–Z a–z 0–9 - _ . ! ~ * ' ( )
; / ? : @ & = + $ , #
The characters on the second line are characters that may be part of the URI syntax, and are only escaped by
encodeURIComponent() do not encode the characters
-.!~*'(), known as "unreserved marks", which do not have a reserved purpose but are allowed in a URI "as is". (See RFC2396)
encodeURI() function does not encode characters that have special meaning (reserved characters) for a URI. The following example shows all the parts that a URI can possibly contain. Note how certain characters are used to signify special meaning: