JavaScript encodeURI() Function
The encodeURI() function encodes a URI.
Note: The encodeURI() function does not encode characters like: , / ? : @ & = + $ #
Syntax
encodeURI(uri)
Parameters
| Parameter | Description |
|---|---|
| uri | Required. The URI to encode. |
Return Value
A string representing the encoded URI.
Description
The encodeURI() function is used to encode a URI. This function encodes special characters, except for: , / ? : @ & = + $ #
If you want to encode these characters as well, please use encodeURIComponent().
This function encodes characters by replacing each instance of certain characters with one, two, three, or four escape sequences representing the UTF-8 encoding of the character.
Technical Details
| JavaScript Version: | ECMAScript 1 |
|---|
Browser Support
encodeURI() is a feature of ECMAScript1 (ES1).
All browsers fully support ES1 (JavaScript 1997):
| Yes | Yes | Yes | Yes | Yes | Yes |
More Examples
Example
Encode a URI:
var uri = "my test.asp?name=stΓ₯le&car=saab";
var res = encodeURI(uri);
The result of res will be:
my%20test.asp?name=st%C3%A5le&car=saab
Try it yourself:
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var uri = "my test.asp?name=stΓ₯le&car=saab";
var res = encodeURI(uri);
document.getElementById("demo").innerHTML = res;
}
</script>
Example
What is the difference between encodeURI() and encodeURIComponent()?
encodeURIComponent() will also encode these characters: , / ? : @ & = + $ #
var uri = "https://w3schools.com/my test.asp?name=stΓ₯le&car=saab";
var enc = encodeURI(uri);
var enc2 = encodeURIComponent(uri);
The result of enc and enc2 will be:
enc: https://w3schools.com/my%20test.asp?name=st%C3%A5le&car=saab enc2: https%3A%2F%2Fw3schools.com%2Fmy%20test.asp%3Fname%3Dst%C3%A5le%26car%3Dsaab
Try it yourself:
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var uri = "https://w3schools.com/my test.asp?name=stΓ₯le&car=saab";
var enc = encodeURI(uri);
var enc2 = encodeURIComponent(uri);
var txt = "encodeURI(): " + enc + "<br>" + "<br>" + "encodeURIComponent(): " + enc2;
document.getElementById("demo").innerHTML = txt;
}
</script>
YouTip