JavaScript encodeURIComponent() Function
JavaScript Global Functions
Definition and Usage
The encodeURIComponent() function encodes a string as a URI component.
This method does not encode ASCII letters and digits, nor does it encode these ASCII punctuation marks: - _ . ! ~ * ' ( ).
Other characters (such as the punctuation marks used to separate URI components: ; / ? : @ & = + $ , #) are replaced by one or more hexadecimal escape sequences.
Syntax
encodeURIComponent(uri)
| Parameters | Description |
|---|---|
| uri | Required. A string containing a URI component or other text to be encoded. |
Browser support





All major browsers support the encodeURIComponent() function
Example
Example
Use encodeURIComponent() to encode a URI:
var
uri
=
"
https://www.runoob.com/my test.php?name=ståle&car=saab
"
;
document
.
write
(
encodeURIComponent
(
uri
)
)
;
The output of the above example is:
https%3A%2F%2Fwww.runoob.com%2Fmy%20test.php%3Fname%3Dst%C3%A5le%26car%3Dsaab
Try it »
JavaScript Global Functions