美文网首页
encodeURI()与encodeURIComponent()

encodeURI()与encodeURIComponent()

作者: 谢大见 | 来源:发表于2018-07-31 10:27 被阅读0次

encodeURI , encodeURIComponent ,escape 三者都是js对字符串进行编码,主要对比一下区别

escape(str)

除了 ASCII 字母、数字和特定的符号外,对传进来的字符串全部进行转义编码,因此如果想对URL编码,最好不要使用此方法。

escape不会编码的字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z

使用场景:最好是单纯对非URI的string需要编码的时候使用它

ECMAScript 3中已经不提倡使用这个函数了,所以我们还是少用吧

encodeURI(uri)

encodeURI()对 URI 进行完整的编码,

encodeURI不会进行编码的字符有82个 :!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z

具有特殊含义的 ASCII 标点符号(, / ? : @ & = + $ #)不会进行转义的

使用encodeURI()编码后的结果是除了空格之外的其他字符都原封不动,只有空格被替换成了%20,encodeURI主要用于直接赋值给地址栏

ocation.href=encodeURI("http://www.baidu.com/")

encodeURIComponent(str)

encodeURIComponent:不会进行编码的字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z

encodeURIComponent编码更很一些,被编码后的字符都变成%xx这样的16进制形式

encodeURIComponent() 方法在编码单个 URIComponent(指请求参 数)应当是最常用的,它可以将参数中的中文、特殊字符进行转义,而不会影响整个URL

location.href="http://www.baidu.com?name="+encodeURIComponent("xie")

参考:
https://blog.csdn.net/baidu_33033415/article/details/62882479
https://blog.csdn.net/luman1991/article/details/54980722

相关文章

网友评论

      本文标题:encodeURI()与encodeURIComponent()

      本文链接:https://www.haomeiwen.com/subject/qjucvftx.html