Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

encodeURI() — encoding a complete URI

encodeURI() encodes characters in a complete URI while leaving some delimiters unchanged. It returns a new string using UTF-8 percent-encoding; it is not intended for encoding individual parameter values.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

encodeURI(uri)

encodeURI(uri) is a global ECMAScript function, not a browser API. It converts its argument to a string and returns a new string, replacing selected characters with %HH sequences representing UTF-8 bytes. A space becomes %20, not +.

It leaves ASCII letters, digits, and -_.!~*'();/?:@&=+$,# unchanged. The characters &, =, and # therefore retain their special meaning, but the function does not preserve the structure of every valid URI. For example, it percent-encodes the square brackets around an IPv6 literal: http://[::1]/ becomes http://%5B::1%5D/, a URL with an invalid host. For an individual parameter value, use encodeURIComponent(), which also encodes &, =, and #.

If you use encodeURIComponent() to encode a single path segment, reject values of exactly . or .. before encoding. The function leaves them unchanged, so a URL parser can normalize them as dot segments instead of preserving them as literal path data. A .. segment can remove the preceding path segment.

The function does not validate an address or establish its safety. It encodes % as %25, so encoding again changes existing escape sequences: %20 becomes %2520. An unpaired UTF-16 surrogate causes URIError; a valid surrogate pair is encoded as one code point.

The example shows which delimiters remain unchanged in the given URL, how é and spaces are encoded, and the difference between encoding a complete URI and encoding one of its components.

Examples

const uri = "https://example.com/café menu?q=tea&size=large#top";
console.log(encodeURI(uri));
console.log(encodeURIComponent("tea&size=large"));
console.log(encodeURI("%20"));

Result

https://example.com/caf%C3%A9%20menu?q=tea&size=large#top
tea%26size%3Dlarge
%2520

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources