escape、encodeURI 和 encodeURIComponent 的区别


Posted in Javascript onMarch 02, 2009

escape() 方法

MSDN JScript Reference中如是说:

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格式返回一个包含传入参数内容的string类型的值。 Escape方法会将传入参数中所有的空格、标点符号、重音字符以及其它任何非ASCII字符替换为%xx的编码形式,其中xx与其所表示的字符的16进制数表示形式相同。如空格字符的16进制表示形式为0x20,则此时xx应为20,即escape(‘ ') 返回“%20”。

Mozilla Developer Core Javascript Guide中如是说:

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 Latin字符集中的字符组成的参数,返回其16进制编码。相对应的,unescape方法则能将16进制编码形式的参数转化成为其ASCII码形式。

encodeURI()方法

MSDN JScript Reference中如是说:

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方法的编码结果传递给decodeURI方法作参数,则能得到原始的未编码的字符串。需要注意到是encodeURI方法不编码如下字符":", "/", ";", and "?"。如果想要编码这些字符,请使用encodeURIComponent方法。

Mozilla Developer Core Javascript Guide中如是说:

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编码来表示这个字符的escape序列来编码一个URI。如 ~!@#$%^&*(){}[]=:/,;?+\'"\\ 将被替换为 ~!@#$%25%5E&*()%7B%7D%5B%5D=:/,;?+'%22%5C

encodeURIComponent()方法

MSDN JScript Reference中如是说:

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方法返回一个编码过的URI。如果将encodeURIComponent方法的编码结果传递给encodeURIComponent方法作参数,则能得到原始的未编码的字符串。因为encodeURIComponent方法会编码所有的字符,所以如果待编码的字符串是用来表示一个路径(如/dir1/dir2/index.htm)时,就一定要小心使用了。‘/'符号会被其编码之后,将不再是一个有效的路径标识符,所以不能被web服务器正确地识别。当字符串包含一个单独的URI component(指?后面的请求参数)的时候,请使用此方法。

Mozilla Developer Core Javascript Guide中如是说:

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.

鄙人译:通过将每个属于特定的字符集合的字符替换为一个、两个或者三个(为什么是“一个、两个或者三个”本人也没有搞懂,望高人赐教)使用UTF-8编码来表示这个字符的escape序列来编码一个URIComponent。

有什么区别?何时使用?

通过上面的介绍可以看出,MS的文档明显要比Mozilla详细、易懂一些,但是它们表达的都是一个意思。但是escape(), encodeURI()和 encodeURIComponent()有什么异同,它们分别适用于那种特定的情况呢?

escape方法并不编码字符+。而我们知道,在用户提交的表单字段中,如果有空格,则会被转化为+字符,而服务器解析的时候则会认为+号代表空格。由于这个缺陷,escape方法并不能正确地处理所有的非ASCII字符,你应当尽量避免使用escape方法,取而代之,你最好选择encodeURIComponent()方法。

escape()不编码的字符:@*/+

相对于使用escape方法,使用encodeURI方法会显得更专业一些。当你需要编码一整个URI的时候,你可以使用此方法,因为URI中的合法字符都不会被编码转换。需要注意到是字符'也是URI中的合法字符,所以也不会被编码转换。

encodeURI() 不编码的字符: ~!@#@{content}*()=:/,;?+'

encodeURIComponent方法在编码单个URIComponent(指请求参数)应当是最常用的。需要注意到是字符'也是URI中的合法字符,所以也不会被编码转换。

encodeURIComponent()不编码的字符: ~!*()'

Javascript 相关文章推荐
jQuery的animate函数学习记录
Aug 08 Javascript
JS实现文字掉落效果的方法
May 06 Javascript
Javascript闭包与函数柯里化浅析
Jun 22 Javascript
javascript时间差插件分享
Jul 18 Javascript
JavaScript中this的用法及this在不同应用场景的作用解析
Apr 13 Javascript
JavaScript实现购物车基本功能
Jul 21 Javascript
详解layui弹窗父子窗口之间传参数的方法
Jan 16 Javascript
QQ跳转支付宝并自动领红包脚本(最新)
Jun 22 Javascript
详解JavaScript栈内存与堆内存
Apr 04 Javascript
微信小程序云开发之使用云函数
May 17 Javascript
js中的面向对象之对象常见创建方法详解
Dec 16 Javascript
从表单校验看JavaScript策略模式的使用详解
Oct 17 Javascript
javascript 文档的编码问题解决
Mar 01 #Javascript
jQuery Div中加载其他页面的实现代码
Feb 27 #Javascript
jQuery 使用个人心得
Feb 26 #Javascript
javascript div 弹出可拖动窗口
Feb 26 #Javascript
javascript URL锚点取值方法
Feb 25 #Javascript
javascript 特殊字符串
Feb 25 #Javascript
javascript 密码强弱度检测万能插件
Feb 25 #Javascript
You might like
PHP安装攻略:常见问题解答(二)
2006/10/09 PHP
谈谈新手如何学习PHP 默默经典版本
2009/08/04 PHP
php+ajax无刷新上传图片实例代码
2015/11/17 PHP
PHP使用GD库输出汉字的方法【测试可用】
2016/11/10 PHP
javaScript 读取和设置文档元素的样式属性
2009/04/14 Javascript
JQuery 1.3.2以上版本中出现pareseerror错误的解决方法
2011/01/11 Javascript
js全选实现和判断是否有复选框选中的方法
2015/02/17 Javascript
EasyUI实现二级页面的内容勾选的方法
2015/03/01 Javascript
JQuery中层次选择器用法实例详解
2015/05/18 Javascript
jquery 中ajax执行的优先级
2015/06/22 Javascript
纯js模拟div层弹性运动的方法
2015/07/27 Javascript
Javascript学习之谈谈JS的全局变量跟局部变量(推荐)
2016/08/28 Javascript
jQuery EasyUI常用数据验证汇总
2016/09/18 Javascript
浅谈jquery的html方法里包含特殊字符的处理
2016/11/30 Javascript
Bootstrap的popover(弹出框)2秒后定时消失的实现代码
2017/02/27 Javascript
详解Vue中添加过渡效果
2017/03/20 Javascript
jQuery查找dom的几种方法效率详解
2017/05/17 jQuery
浅谈原生JS中的延迟脚本和异步脚本
2017/07/12 Javascript
JS常见DOM节点操作示例【创建 ,插入,删除,复制,查找】
2018/05/14 Javascript
解决jquery有正确返回值但不执行success函数的问题
2018/08/20 jQuery
Vue+Element实现网页版个人简历系统(推荐)
2019/12/31 Javascript
js实现简单的秒表
2020/01/16 Javascript
[38:27]完美世界DOTA2联赛PWL S2 Forest vs FTD.C 第二场 11.26
2020/11/30 DOTA
Python struct.unpack
2008/09/06 Python
python连接mysql调用存储过程示例
2014/03/05 Python
python Django中models进行模糊查询的示例
2019/07/18 Python
linux下python中文乱码解决方案详解
2019/08/28 Python
pandas实现DataFrame显示最大行列,不省略显示实例
2019/12/26 Python
Pytorch 实现计算分类器准确率(总分类及子分类)
2020/01/18 Python
Sneaker Studio乌克兰:购买运动鞋
2018/03/26 全球购物
临床医学应届生求职信
2013/11/06 职场文书
应届生财务管理求职信
2013/11/06 职场文书
出生公证委托书
2014/04/03 职场文书
小学中等生评语
2014/12/29 职场文书
宣传稿格式范文
2015/07/23 职场文书
副校长2015年教育教学工作总结
2015/07/27 职场文书