JavaScript Reference ยท Global Functions and Values
The decodeURI() function
Reference for decodeURI(): decoding a whole URL while leaving reserved characters escaped, how it differs from decodeURIComponent(), and URIError cases.
decodeURI() reverses encodeURI(). It replaces percent-escapes with the characters they stand for, with one important exception: escapes that stand for reserved URL characters (; / ? : @ & = + $ , #) are left as they are. This keeps a decoded URL from changing its meaning.
For example %2F stays %2F, because turning it into / would add a new path segment.
decodeURI("https://example.com/my%20page?q=caf%C3%A9"); // "https://example.com/my page?q=cafรฉ"
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
decodeURI(encodedURI)
| Name | Description |
|---|---|
encodedURI |
The URL text to decode. Non-strings are converted to strings first. |
Return value: a new string in which percent-escapes are decoded, except those that would produce reserved URL characters.
Exceptions: a URIError if a % is not followed by two hex digits, or if the bytes are not valid UTF-8.
Usage notes
- Reserved escapes survive. Escapes for
; / ? : @ & = + $ , #stay in their%XXform. UsedecodeURIComponent()if you want them decoded. - Use it on whole URLs, for example to display a readable address. For a single query value,
decodeURIComponent()is the right tool. - Case of the hex digits is irrelevant when decoding:
%c3%a9and%C3%A9both giveรฉ. - Malformed text throws; wrap input from outside your control in
try...catch. - Do not use decoded URLs for requests. A readable address with spaces or non-ASCII characters is for display; keep the encoded form for navigation and fetching.
- For query values,
new URL(text).searchParams.get("name")returns the decoded value directly.
Browser support
decodeURI() is available in every browser and runtime.
Related
encodeURI(),decodeURIComponent(),URL- Lessons: Strings, Errors