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 these ASCII punctuation marks: - _ . ! ~ * ' ( ).

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

Syntax

encodeURIComponent(uri)

Parameter Description
uri Required. A string containing a URI component or other text to be encoded.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the encodeURIComponent() function


Examples

Example

Encode a URI using encodeURIComponent():

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

Output of the above example:

https%3A%2F%2Fwww.example.com%2Fmy%20test.php%3Fname%3Dst%C3%A5le%26car%3Dsaab

Try it Yourself »

函数 参考手册JavaScript Global Functions Other Extensions