区分escape、encodeURI和encodeURIComponent
来源:互联网 发布:手机赚钱软件微信提现 编辑:程序博客网 时间:2024/06/08 07:02
转载自: http://www.cnblogs.com/season-huang/p/3439277.html
escape
简单来说,escape
是对字符串(string)进行编码(而另外两种是对URL),作用是让它们在所有电脑上可读。
编码之后的效果是%XX
或者%uXXXX
这种形式。
其中 ASCII字母、数字、@*/+ ,这几个字符不会被编码,其余的都会。
最关键的是,当你需要对URL编码时,请忘记这个方法,这个方法是针对字符串使用的,不适用于URL。
encodeURI, encodeURIComponent
对URL编码是常见的事,所以这两个方法应该是实际中要特别注意的。
它们都是编码URL,唯一区别就是编码的字符范围,其中 encodeURI
方法不会对下列字符编码 ASCII字母、数字、~!@#$&*()=:/,;?+’ encodeURIComponent
方法不会对下列字符编码 ASCII字母、数字、~!*()’
所以encodeURIComponent比encodeURI
编码的范围更大。
实际例子来说,encodeURIComponent
会把 http://
编码成http%3A%2F%2F
而encodeURI
却不会。
例子
如果只是编码字符串,不和URL有半毛钱关系,那么用escape。
如果你需要编码整个URL,然后需要使用这个URL,那么用encodeURI。
比如encodeURI("http://www.cnblogs.com/season-huang/some other thing");
编码后会变为http://www.cnblogs.com/season-huang/some%20other%20thing;
其中,空格被编码成了%20
。但是如果你用了encodeURIComponent
,那么结果变为http%3A%2F%2Fwww.cnblogs.com%2Fseason-huang%2Fsome%20other%20thing
看到了区别吗,连 “/” 都被编码了,整个URL已经没法用了。当你需要编码URL中的参数的时候,那么
encodeURIComponent
是最好方法。
var param = "http://www.cnblogs.com/season-huang/"; //param为参数param = encodeURIComponent(param);var url = "http://www.cnblogs.com?next=" + param;console.log(url) //"http://www.cnblogs.com?next=http%3A%2F%2Fwww.cnblogs.com%2Fseason-huang%2F"
看到了把,参数中的 “/” 可以编码,如果用encodeURI肯定要出问题,因为后面的/是需要编码的。
- 区分escape、encodeURI和encodeURIComponent
- 简单明了区分escape、encodeURI和encodeURIComponent
- 简单明了区分escape、encodeURI和encodeURIComponent
- 简单明了区分escape、encodeURI和encodeURIComponent
- 简单明了区分escape、encodeURI和encodeURIComponent
- 简单明了区分escape、encodeURI和encodeURIComponent
- escape encodeURI 和encodeURIComponent
- escape encodeURI 和encodeURIComponent
- escape、encodeURI和encodeURIComponent
- encodeURI(),encodeURIComponent()和escape()
- escape(),encodeURI()和encodeURIComponent()区别
- encodeURIComponent 和 encodeURI 和 escape 简要汇总
- Js中escape(),encodeURI()和encodeURIComponent()比较
- escape、encodeURI 和encodeURIComponent 的区别
- javascript中escape、encodeURI和encodeURIComponent
- escape、encodeURI 和encodeURIComponent 的区别
- escape、encodeURI 和 encodeURIComponent 的区别
- Js中escape(),encodeURI()和encodeURIComponent()
- EF 存储过程(上)
- 20170406 bpi_a64_android的编译配置文件排版v1.2(分色排版)
- starUML for MAC 破解方法
- 如何在pycharm中使用配置好的virtualenv环境
- PowerDesigner导出模型为sql文件时,执行generate database 报错
- 区分escape、encodeURI和encodeURIComponent
- EF支持复杂类型的实现
- JSON转Date的序列化问题之解决方案(Java)
- [每日问答]模型融合的方式有哪些?
- 高速CAN总线 负载阻抗匹配方法的介绍
- 待解决
- vuejs选中当前样式active
- C -array
- Algorithm——Quicksort(快速排序)