JavaScript encodeURIComponent() 函数
定义和用法
encodeURIComponent() 函数对 URI 组件进行编码。
此函数对特殊字符进行编码。此外,它还对以下字符进行编码: , / ? : @ & = + $ #
提示:请使用 decodeURIComponent() 函数对编码的 URI 组件进行解码。
实例
对 URI 进行编码:
var uri = "https://w3school.com.cn/my test.asp?name=ståle&car=saab"; var res = encodeURIComponent(uri);
语法
encodeURIComponent(uri)
参数值
| 参数 | 描述 |
|---|---|
| uri | 必需。要编码的 URI。 |
技术细节
| 返回值: | 字符串,表示编码后的 URI。 |
|---|
浏览器支持
| 函数 | Chrome | Edge | Firefox | Safari | Opera |
|---|---|---|---|---|---|
| encodeURIComponent() | 支持 | 支持 | 支持 | 支持 | 支持 |