encodeURI

Did you know that you can read content offline by using one of these tools? If you would like to read offline MDN content in another format, let us know by commenting on Bug 665750.

Dash App

Redirected from encodeURI Redirect 1

Summary

Encodes a Uniform Resource Identifier (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).

Syntax

encodeURI(URI)

Parameters

URI
A complete Uniform Resource Identifier.

Description

Assumes that the URI is a complete URI, so does not encode reserved characters that have special meaning in the URI.

encodeURI replaces all characters except the following with the appropriate UTF-8 escape sequences:

Type Includes
Reserved characters ; , / ? : @ & = + $
Unescaped characters alphabetic, decimal digits, - _ . ! ~ * ' ( )
Score #

Note that encodeURI by itself cannot form proper HTTP GET and POST requests, such as for XMLHTTPRequests, because "&", "+", and "=" are not encoded, which are treated as special characters in GET and POST requests. encodeURIComponent, however, does encode these characters. These behaviors are most likely not consistent across browsers.

Note that an error will be thrown if one attempts to encode a surrogate which is not part of a high-low pair, e.g.,

alert(encodeURI('\uD800\uDFFF')); // high-low pair ok
alert(encodeURI('\uD800')); // lone high surrogate throws "URIError: malformed URI sequence"
alert(encodeURI('\uDFFF')); // lone low surrogate throws "URIError: malformed URI sequence"

Also note that if one wishes to follow the more recent RFC3986 for URL's, making square brackets reserved (for IPv6) and thus not encoded when forming something which could be part of a URL (such as a host), the following may help. See https://waybackassets.bk21.net/20120107105049/http://labs.apache.org/webarch/uri/rfc/rfc3986.html (via Internet Archive).

function fixedEncodeURI (str) {
    return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']');
}

See also

Tags (3)