比较escape、encodeURI、encodeURIComponent
估计很多前端工程师并不清楚escape,encodeURI, encodeURIComponent的区别,也不知道什么时候该用哪个方法,以及这些方法为什么要被用到,下面我主要来阐述一下这三个方法的区别以及用法。
escape 方法:
The escape method returns a string value (in Unicode format) that contains the contents of [the argument]. All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding, where xx is equivalent to the hexadecimal number representing the character. For example, a space is returned as "%20."
escape 方法返回一个包含参数内容的UNICODE格式的字符串值。所有的“空格”、“标点”、“重音字符”以及任何非ASCII字符都会被替换为带着%xx 编码,这里的xx是一个表示原来字符的16进制数字。例如一个空格会被替换为%20
The escape and unescape functions let you encode and decode strings. The escape function returns the hexadecimal encoding of an argument in the ISO Latin character set. The unescape function returns the ASCII string for the specified hexadecimal encoding value.
escape 和 unescape 方法让你可以对字符串编码和解码。 escape 调用方法将返回对ISO拉丁字符参数的16进制编码,而 unescape 方法会为指定的16进制编码返回ASCII 字符。
看看例子吧
- escape('~!@#$%^&*(){}[]=:/,;?+\'"\\')
- //%7E%21@%23%24%25%5E%26*%28%29%7B%7D%5B%5D%3D%3A/%2C%3B%3F+%27%22%5C
encode 方法:
The encodeURI method returns an encoded URI. If you pass the result to decodeURI, the original string is returned. The encodeURI method does not encode the following characters: ":", "/", ";", and "?". Use encodeURIComponent to encode these characters.
encodeURI 方法返回一个编码的 URI, encodeURI 不对以下字符编码:
":", "/", ";", and "?" 而可以用encodeURIComponent对这些字符编码
下面说的更详细一些
Encodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character.
通过替换每一个字符实例成一个或是两三个编码过的UTF-8字符,来对URI编码。
例子:
- encodeURI('~!@#$%^&*(){}[]=:/,;?+\'"\\')
- //~!@#$%25%5E&*()%7B%7D%5B%5D=:/,;?+'%22%5C
encodeURIComponent 方法:
The encodeURIComponent method returns an encoded URI. If you pass the result to decodeURIComponent, the original string is returned. Because the encodeURIComponent method encodes all characters, be careful if the string represents a path such as /folder1/folder2/default.html. The slash characters will be encoded and will not be valid if sent as a request to a web server. Use the encodeURI method if the string contains more than a single URI component.
encodeURIComponent会对更多的字符进行编码,包括表示路径的"/",所以当使用时,需要谨慎
Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character.
这个解释和encode一样,实际上encodeURI 和 encodeURIComponent 差别就是一个是对更多的字符编码,而一个只是对URI部分编码。
同样字符串的例子:
- escape('~!@#$%^&*(){}[]=:/,;?+\'"\\')
- //%7E%21@%23%24%25%5E%26*%28%29%7B%7D%5B%5D%3D%3A/%2C%3B%3F+%27%22%5C
- encodeURI('~!@#$%^&*(){}[]=:/,;?+\'"\\');
- //~!@#$%25%5E&*()%7B%7D%5B%5D=:/,;?+'%22%5C
- encodeURIComponent('~!@#$%^&*(){}[]=:/,;?+\'"\\');
- //~!%40%23%24%25%5E%26*()%7B%7D%5B%5D%3D%3A%2F%2C%3B%3F%2B'%22%5C
总结:
该怎么使用呢?
escape:
escape 不会编码的字符:@*/+
escape方法不回编码+字符,+字符在服务器端会被解释成空格,这点和通过表达提交一样。
由于escape有这样的缺点,和它不能很好的正确处理非ASCII字符的事实,我们应该尽量避免(对URI)使用escape,最好的方式是encodeURIComponent。
encodeURI:
encodeURI 不会编码的字符很多,有:~!@#$&*()=:/,;?+'
在对一段URI编码来说,encodeURI方法比escape方法更专业一些。当你需要编码一整个URI的时候,你可以使用此方法,因为URI中的合法字符都不会被编码转换。需要注意到是字符’也是URI中的合法字符,所以也不会被编码转换。
encodeURIComponent:
encodeURIComponent不会编码的字符: ~!*()'
encodeURIComponent方法在编码单个URIComponent(指请求参数)应当是最常用的。需要注意到是字符’也是URI中的合法字符,所以也不会被编码转换。
我的小结:
其实前端最常用的是encodeURIComponent,因为这个方法会把URI中的queryString中的非法字符编码,这样通过get请求向服务器端传递参数的时候不会出错。
escape 在后端和前端的接口规约里常常会碰到,但是一般不需要前端在本地escape,一般是后端接口吐出 escape后的数据,前端unescape。
此文章是翻译而来
原文:http://xkr.us/articles/javascript/encode-compare/
关于URL,URI不太明白的同学可以看看 http://www.ibm.com/developerworks/cn/xml/x-urlni.html
这篇文章
比较escape、encodeURI、encodeURIComponent的更多相关文章
- url的三个js编码函数escape(),encodeURI(),encodeURIComponent()简介
url的三个js编码函数escape(),encodeURI(),encodeURIComponent()简介 2014年10月12日 16806次浏览 引子 浏览器URl地址,上网一定会用到,但是浏 ...
- escape,encodeURI,encodeURIComponent函数比较
escape,encodeURI,encodeURIComponent函数比较 js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数 ...
- escape,encodeURI,encodeURIComponent
JavaScript/js中,有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,d ...
- Flex中escape/encodeURI/encodeURIComponent的区别
Flex中提供了三种转码函数,各有各的区别, escape,encodeURI,encodeURIComponent 这三个函数不仅在flex中有道运用在javascript中同样的含义 ,今天我仔细 ...
- url的三个js编码函数escape(),encodeURI(),encodeURIComponent()简介【转】
引子 浏览器URl地址,上网一定会用到,但是浏览器地址有中文或者浏览器url参数操作的时候,经常会用到encodeURIComponent()和decodeURIComponent()以及encode ...
- JavaScript中有三个可以对字符串编码的函数,分别是: escape(),encodeURI(),encodeURIComponent()
JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decod ...
- JavaScript中有对字符串编码的三个函数:escape,encodeURI,encodeURIComponent
JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decod ...
- escape,encodeURI,encodeURIComponent的区别
escape是对字符串进行编码而另外两种是对URL. encodeURI方法不会对下列字符编码 ASCII字母 数字 ~!@#$&*()=:/,;?+'encodeURIComponent方法 ...
- js中的三个编码函数:escape,encodeURI,encodeURIComponent
1. eacape(): 该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: * @ - _ + . / .其他所有的字符都会被转义序列替换.其它情况下es ...
- 浏览器编码的函数简介escape(),encodeURI(),encodeURIComponent()
1.escape() escape()是js编码函数中最古老的一个.虽然这个函数现在已经不提倡使用了,但是由于历史原因,很多地方还在使用它,所以有必要先从它讲起. 实际上,escape()不能直接用于 ...
随机推荐
- 【剑指offer 面试题17】合并两个排序的链表
思路: 比较两个链表端点值的大小,通过递归的方式排列. #include <iostream> using namespace std; struct ListNode { int val ...
- <译>Selenium Python Bindings 3 - Navigating
当你想要通过webdriver导航到一个链接,正常的方式点是通过调用get方法: driver.get("http://www.google.com") Interacting w ...
- Tableau学习笔记之五
计算用户自定义字段,虽然在Tableau软件中已经加入了很多的数值操作运算,比如平均值,最大值等,但是可以自定义自己需要的数值操作运算. 数值操作可以有以下:预定义函数,百分比,总计,分级等等 1.直 ...
- 选择学习Pomelo
我之前的项目都是基于http做网络通信,但是做多玩家同时对战的游戏,http短连接不支持服务器的push是个问题,这样客户端就没办法收到服务器的消息. 最简单的方法是定时发起request询问服务器, ...
- 简易CSS3 Tab菜单 Tab切换滑块动画
今天要分享一款简易的CSS3 Tab菜单,这款Tab菜单在切换的时候内容块出现飞入飞出的动画效果,尽管看起来非常简单,但是你完全可以在上面定制自己喜欢的Tab菜单.前面也分享过一些Tab菜单,像CSS ...
- leetcode—Plus one
1.题目描述 Given a number represented as an array of digits, plus one to the number. 2.解法分析 不要被常规思路限制住 ...
- java StreamTokenizer使用
注意:用JAVA解题一般用Scanner类来进行输入,但对时间要求严格的题,用它可能会超时,我.解POJ1823的时候就遇到这样的问题,后改用StreamTokenizer类进行输入,就过了.看来后者 ...
- Adress
流水账的写法:因为不想让亲爱的你只看开头就关掉了我辛苦的劳作.流水账的好处是:便于逻辑的理解 http://software.intel.com/zh-cn/blogs/2014/01/20/cent ...
- 第二百九十四天 how can I 坚持
这是怎么了,好难受,晚上都没吃饭,全身都疼.该咋办. 其实,真的是身体最重要. 洗洗睡了.好难受.
- Spark RDD概念学习系列之RDD的重要内部属性(十五)
RDD的重要内部属性 通过 RDD 的内部属性,用户可以获取相应的元数据信息.通过这些信息可以支持更复杂的算法或优化. 1)分区列表:通过分区列表可以找到一个 RDD 中包含的所有分区及其所在地址. ...