估计很多前端工程师并不清楚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. 【PHP】Windows环境Hello World

    转自:http://www.cnblogs.com/wangkangluo1/archive/2011/07/19/2110943.html 一 下载 XAMPP下载地址: https://sourc ...

  2. poj 1651 http://poj.org/problem?id=1651

      http://poj.org/problem?id=1651Multiplication Puzzle   Time Limit: 1000MS   Memory Limit: 65536K To ...

  3. CABasicAnimation

    几个可以用来实现热门APP应用PATH中menu效果的几个方法 +(CABasicAnimation *)opacityForever_Animation:(float)time //永久闪烁的动画 ...

  4. firebug console使用

    Firebug内置一个console对象,提供5种方法,用来显示信息. console.log("Hello World") console.info("这是info&q ...

  5. C++实现网格水印之调试笔记(四)—— 完成嵌入

    接下来的问题是,当模型是对称的时候,结果是符合预期的,但是当模型是不对称的时候,结果是错误的,如下: 输入: 顶点:233 输出: 这又是什么鬼...,我的马呢!!! 看来逻辑上还是有错误 注意这时候 ...

  6. 如何利用WordPress创建自定义注册表单插件

    来源:http://www.ido321.com/1031.html 原文:Creating a Custom WordPress Registration Form Plugin 译文:创建一个定制 ...

  7. 【暑假】[实用数据结构]UVAlive 3135 Argus

    UVAlive 3135 Argus Argus Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %l ...

  8. 我用dedecms有感

    ---恢复内容开始--- 最近接了一个私单,简单的学校网站,注意,我一看上去是感觉很快,仿站,对方说这个东西你三天就能搞定啦,我也这么想的 (没经验啊) 接下来,我想都没想就用dedecms去做,之前 ...

  9. 【Hadoop代码笔记】Hadoop作业提交之Child启动map任务

    一.概要描述 在上篇博文描述了TaskTracker启动一个独立的java进程来执行Map或Reduce任务.在本篇和下篇博文中我们会关注启动的那个入口是org.apache.hadoop.mapre ...

  10. Todolist

    UValive 6041(KD tree) UValive 6042(DP) UValive 6044(图论)