自学内容网 自学内容网

encodeURI与encodeURIComponent的区别

encodeURI与encodeURIComponent的区别

decodeURI(URIstring):  //可对 encodeURI() 函数编码过的 URI 进行解码
 
// 对字符串进行URI 编码,该方法不会对 ASCII 字母和数字进行编码
// 也不会对具有特殊含义的 ASCII 标点符号,如:   :;/?:@&=+$,# 进行编码
// 也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( )
encodeURI(URIstring)
 
// 例如
var test1="http://www.w3school.com.cn/My first/?中国"
 
encodeURI(test1)     //http://www.w3school.com.cn/My%20first/?%E4%B8%AD%E5%9B%BD
decodeURI(test1)     //http://www.w3school.com.cn/My first/?中国

如果要对比如 ? 和 # 的 字符进行URI 编码,则应当使用 encodeURIComponent() 方法分别对各组件进行编码。

decodeURIComponent(URIstring)://对encodeURIComponent() 函数编码的 URI 进行解码
 
//不会对 ASCII 字母和数字进行编码
//也不会对这些 ASCII 标点符号进行编码:!-_.!~*'()
//但是其他字符,比如 :;/?:@&=+$,# 这些用于分隔 URI 组件的标点符号),都会被由一个或多个十六进制的转义序列替换
encodeURIComponent(URIstring)
 
例如:
var test1="http://www.w3school.com.cn/My first/?中国"
 
encodeURIComponent(test1) //http%3A%2F%2Fwww.w3school.com.cn%2FMy%20first%2F%3F%E4%B8%AD%E5%9B%BD
decodeURIComponent(test1)  //http://www.w3school.com.cn/My first/?中国


原文地址:https://blog.csdn.net/aexwx/article/details/142757285

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!