估计很多前端工程师并不清楚escape,encodeURI, encodeURIComponent的区别,也不知道什么时候该用哪个方法,以及这些方法为什么要被用到,下面我主要来阐述一下这三个方法的区别以及用法。 
escape 方法:

引用
MSDN JScript: 
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

引用
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拉丁字符参数的16进制编码,而 unescape 方法会为指定的16进制编码返回ASCII 字符。 
看看例子吧

  1. escape('~!@#$%^&*(){}[]=:/,;?+\'"\\')
  2. //%7E%21@%23%24%25%5E%26*%28%29%7B%7D%5B%5D%3D%3A/%2C%3B%3F+%27%22%5C

encode 方法:

引用
MSDN JScript: 
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对这些字符编码 
下面说的更详细一些

引用
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字符,来对URI编码。 
例子:

  1. encodeURI('~!@#$%^&*(){}[]=:/,;?+\'"\\')
  2. //~!@#$%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会对更多的字符进行编码,包括表示路径的"/",所以当使用时,需要谨慎

引用
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.

这个解释和encode一样,实际上encodeURI 和 encodeURIComponent 差别就是一个是对更多的字符编码,而一个只是对URI部分编码。 
同样字符串的例子:

  1. escape('~!@#$%^&*(){}[]=:/,;?+\'"\\')
  2. //%7E%21@%23%24%25%5E%26*%28%29%7B%7D%5B%5D%3D%3A/%2C%3B%3F+%27%22%5C
  3. encodeURI('~!@#$%^&*(){}[]=:/,;?+\'"\\');
  4. //~!@#$%25%5E&*()%7B%7D%5B%5D=:/,;?+'%22%5C
  5. encodeURIComponent('~!@#$%^&*(){}[]=:/,;?+\'"\\');
  6. //~!%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的更多相关文章

  1. url的三个js编码函数escape(),encodeURI(),encodeURIComponent()简介

    url的三个js编码函数escape(),encodeURI(),encodeURIComponent()简介 2014年10月12日 16806次浏览 引子 浏览器URl地址,上网一定会用到,但是浏 ...

  2. escape,encodeURI,encodeURIComponent函数比较

    escape,encodeURI,encodeURIComponent函数比较 js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数 ...

  3. escape,encodeURI,encodeURIComponent

    JavaScript/js中,有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,d ...

  4. Flex中escape/encodeURI/encodeURIComponent的区别

    Flex中提供了三种转码函数,各有各的区别, escape,encodeURI,encodeURIComponent 这三个函数不仅在flex中有道运用在javascript中同样的含义 ,今天我仔细 ...

  5. url的三个js编码函数escape(),encodeURI(),encodeURIComponent()简介【转】

    引子 浏览器URl地址,上网一定会用到,但是浏览器地址有中文或者浏览器url参数操作的时候,经常会用到encodeURIComponent()和decodeURIComponent()以及encode ...

  6. JavaScript中有三个可以对字符串编码的函数,分别是: escape(),encodeURI(),encodeURIComponent()

    JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decod ...

  7. JavaScript中有对字符串编码的三个函数:escape,encodeURI,encodeURIComponent

    JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decod ...

  8. escape,encodeURI,encodeURIComponent的区别

    escape是对字符串进行编码而另外两种是对URL. encodeURI方法不会对下列字符编码 ASCII字母 数字 ~!@#$&*()=:/,;?+'encodeURIComponent方法 ...

  9. js中的三个编码函数:escape,encodeURI,encodeURIComponent

    1. eacape(): 该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: * @ - _ + . / .其他所有的字符都会被转义序列替换.其它情况下es ...

  10. 浏览器编码的函数简介escape(),encodeURI(),encodeURIComponent()

    1.escape() escape()是js编码函数中最古老的一个.虽然这个函数现在已经不提倡使用了,但是由于历史原因,很多地方还在使用它,所以有必要先从它讲起. 实际上,escape()不能直接用于 ...

随机推荐

  1. BLOCK专题

    >>定义并使用一个block    返回值(^名字)(参数列表) =^返回值类型(参数列表){表达式};  其中返回值和参数列表可以神略 ,最简单的block是  ^{xxxx}; voi ...

  2. http://blog.csdn.net/jbb0403/article/details/42102527

    http://blog.csdn.net/jbb0403/article/details/42102527

  3. 软件测试技术(五)——Software Review

    本周的测试课上进行了一次同行评审的演练,大家讨论的很热烈,不过我也发现了一些不太理解的过程,如如何进行计划活动,走读.技术评审.正规检视是基于什么目的,并应该在何时进行.我做了一下详细的研究. 首先, ...

  4. jenkins 命令行 CLI jenkins-cli.jar

    部署好jenkins后,一般都是通过jenkins提供的web界面来操作jenkins. 而有些场景则需要通过命令来操作jenkins,例如通过脚本操作jenkins. 在jenkins提供的web界 ...

  5. C ~ 一个串口接收思路

    void uart_rx_isr(void) //接收中断函数 { uchar c; c=SBUF;//c等于接收的字节: switch (recv_state) { : if (c==0x02) / ...

  6. cocos2d-x知识巩固-基础篇(2)

    上一篇博客介绍了整个cocos2dx引擎需要掌握的各个模块,每一个模块实际上往深了研究都有难点,后面我会详细地去分析它的用法.今天我们从第一个模块说起,即渲染模块.首先,为了理解,我们做个类比,说明该 ...

  7. 选择学习Pomelo

    我之前的项目都是基于http做网络通信,但是做多玩家同时对战的游戏,http短连接不支持服务器的push是个问题,这样客户端就没办法收到服务器的消息. 最简单的方法是定时发起request询问服务器, ...

  8. MorningSale 介绍

    MorningSale是一个WEB端的收集门店销售数据,显示销售数据的简单系统,我相信该系统能够有效的提高销售公司在门店销售数据收集 汇总 分析方面的工作效率. 主要功能介绍如下: 1.查看某个店面 ...

  9. Linux下gcc和g++编译helloworld

    linux C(hello world) 1.使用vi/vim进行编写代码并保存为hello_world.c.如下: 1 2 3 4 5 6 /* This is my first C program ...

  10. oc_转_构造对象的方法,以及类的继承

    一.构造方法 (一)构造方法的调用 完整的创建一个可用的对象:Person *p=[Person new]; New方法的内部会分别调用两个方法来完成2件事情: 1) 使用alloc方法来分配存储空间 ...