JavaScript encodeURIComponent() 函數

定義和用法

encodeURIComponent() 函數對 URI 組件進行編碼。

此函數對特殊字符進行編碼。此外,它還對以下字符進行編碼: , / ? : @ & = + $ #

提示:請使用 decodeURIComponent() 函數對編碼的 URI 組件進行解碼。

實例

對 URI 進行編碼:

var uri = "https://codew3c.com/my test.asp?name=stale&car=saab";
var res = encodeURIComponent(uri);

親自試一試

語法

encodeURIComponent(uri)

參數值

參數 描述
uri 必需。要編碼的 URI。

技術細節

返回值: 字符串,表示編碼后的 URI。

瀏覽器支持

函數 Chrome Edge Firefox Safari Opera
encodeURIComponent() 支持 支持 支持 支持 支持