encodeURIComponent()加密
定義和用法
encodeURIComponent() 函數可把字符串作為 URI 組件進行編碼。
語法
encodeURIComponent(URIstring)
參數 | 描述 |
---|---|
URIstring | 必需。一個字符串,含有 URI 組件或其他要編碼的文本。 |
返回值
URIstring 的副本,其中的某些字符將被十六進制的轉義序列進行替換。
說明
該方法不會對 ASCII 字母和數字進行編碼,也不會對這些 ASCII 標點符號進行編碼: - _ . ! ~ * ' ( ) 。
其他字符(比如 :;/?:@&=+$,# 這些用於分隔 URI 組件的標點符號),都是由一個或多個十六進制的轉義序列替換的。
使用方法:
document.write(encodeURIComponent("http://www.w3school.com.cn")+ "<br />") document.write(encodeURIComponent("http://www.w3school.com.cn/My first/")+ "<br />") document.write(encodeURIComponent(",/?:@&=+$#"))
輸出結果:
http%3A%2F%2Fwww.w3school.com.cn
http%3A%2F%2Fwww.w3school.com.cn%2FMy%20first%2F
%2C%2F%3F%3A%40%26%3D%2B%24%23
---------------------------------------
decodeURIComponent()解碼
定義和用法
decodeURIComponent() 函數可對 encodeURIComponent() 函數編碼的 URI 進行解碼。
語法
decodeURIComponent(URIstring)
參數 | 描述 |
---|---|
URIstring | 必需。一個字符串,含有編碼 URI 組件或其他要解碼的文本。 |
返回值
URIstring 的副本,其中的十六進制轉義序列將被它們表示的字符替換。
var test1="http://www.w3school.com.cn/My first/" document.write(encodeURIComponent(test1)+ "<br />") document.write(decodeURIComponent(test1))
輸出結果:
http%3A%2F%2Fwww.w3school.com.cn%2FMy%20first%2F
http://www.w3school.com.cn/My first/