Easy Tutorial
❮ Met Console Groupcollapsed Dom Obj Table ❯

JavaScript encodeURIComponent() Function


Definition and Usage

The encodeURIComponent() function encodes a string as a URI component.

This method does not encode ASCII letters and numbers, nor does it encode these ASCII punctuation marks: - _ . ! ~ * ' ( ).

Other characters (such as :;/?:@&=+$,# which are used to separate URI components) are replaced by one or more hexadecimal escape sequences.

Syntax

Parameter 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

Encode a URI using encodeURIComponent():

var uri = "https://www.tutorialpro.org/my test.php?name=ståle&car=saab";
document.write(encodeURIComponent(uri));

The above example outputs:

https%3A%2F%2Fwww.tutorialpro.org%2Fmy%20test.php%3Fname%3Dst%C3%A5le%26car%3Dsaab
❮ Met Console Groupcollapsed Dom Obj Table ❯