encodeURI() makes a whole web address safe to use. It replaces characters that are not allowed in a URL, such as spaces and letters like e with an accent, with percent codes like %20.
It leaves the parts that give a URL its structure alone: : / ? # & = and a few more. That is the key difference from encodeURIComponent(), which encodes those too.
Remember
encodeURI(uri);
uri -> a complete URL (or a text that looks like one)
returns -> a new string with unsafe characters as %XX codes
NOT encoded: A-Z a-z 0-9 - _ . ! ~ * ' ( )
; , / ? : @ & = + $ #
Encoded: spaces, quotes, < > { } | and non-English letters
Syntax
Call encodeURI() by name and give it a full URL as text. It returns the encoded text and does not change the original.
JavaScript
encodeURI(uri);
Call
What it does
encodeURI("https://example.com/my file.pdf")
The space becomes %20. Result: "https://example.com/my%20file.pdf".
encodeURI("a?b=1&c=2")
Nothing changes, because ? = & are part of the URL structure.
encodeURI("caf\u00e9")
The accented letter becomes its UTF-8 bytes. Result: "caf%C3%A9".
Second pass. The % of %20 is encoded again as %25, so you get %2520. A server that decodes once will see my%20file.txt instead of my file.txt.
Broken Unicode."\uD800" is half of a pair of codes and cannot be turned into bytes. encodeURI() throws a URIError, and the catch block prints its name.
Compatibility
Browser & Runtime Support
encodeURI() arrived with ES3 and has worked in every browser for a very long time. Every Node.js version has it too.
✓ ES3 · Works everywhere
encodeURI()
Supported in all browsers, including old ones, and in Node.js, Deno and Bun.
ES3Standard since
Google ChromeAll versions · Desktop & Mobile
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
OperaAll versions
Full support
Samsung InternetAll versions · Android
Full support
Android WebViewAll versions
Full support
Node.jsAll versions · Server runtime
Supported
DenoAll versions · JavaScript runtime
Supported
BunAll versions · JavaScript runtime
Supported
Internet ExplorerAll versions
Full support
encodeURI()Excellent
Bottom line: Safe to use anywhere. Even Internet Explorer has it. For new code you can also build URLs with the URL object, which encodes parts for you.
Frequently Asked Questions
It turns a complete URL into a safe text by replacing characters that are not allowed, such as spaces and non-English letters, with percent codes like %20. It keeps characters that give the URL its structure, such as : / ? # & and =.
encodeURI() is for a whole URL and keeps the structure characters : / ? # & = + $ , ; @. encodeURIComponent() is for one piece of a URL, such as a search word, and encodes those characters too. Use encodeURIComponent() for values you put inside a query string.
A URL cannot contain a space. %20 is the percent code for the space character (ASCII code 32 in hexadecimal is 20). Forms sometimes use + for a space, but encodeURI() always uses %20.
Because % is the start of every percent code, a plain % in the text must be written as %25. That is also why encoding already encoded text breaks it: %20 becomes %2520.
When the text contains a lone surrogate, which is half of a character from outside the basic range (such as an emoji) without its other half. Valid text, including whole emoji, never throws.