Latest web development tutorials

JavaScript encodeURIComponent () function

Function Reference Manual JavaScript Global Functions

Definition and Usage

encodeURIComponent () function to encode the string as a URI component.

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

Other characters (for example:; /: @ & = + $, # used to separate these components URI punctuation?), Is replaced by one or more hexadecimal escape sequence of.

grammar

encodeURIComponent(uri)

参数 描述
uri 必需。一个字符串,含有 URI 组件或其他要编码的文本。


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support encodeURIComponent () function


Examples

Example

Use encodeURIComponent () to encode the URI:

<script>

var uri="http://w3cschool.cc/my test.php?name=stale&car=saab";
document.write(encodeURIComponent(uri));

</script>

Examples of the above output:

http%3A%2F%2Fw3cschool.cc%2Fmy%20test.php%3Fname%3Dst%C3%A5le%26car%3Dsaab

try it"

Function Reference Manual JavaScript Global Functions