js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent

1、   传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断。

例如:<script language="javascript">document.write('<a href="http://passport.baidu.com/?logout&aid=7& u='+encodeURIComponent("http://cang.baidu.com/bruce42")+'">退出</a& gt;');</script>

2、   进行url跳转时可以整体使用encodeURI

例如:Location.href=encodeURI("http://cang.baidu.com/do/s?word=百度& ct=21");

3、   js使用数据时可以使用escape

[Huoho.Com编辑]

例如:搜藏中history纪录。

4、   escape对0-255以外的unicode值进行编码时输出%u****格式,其它情况下 escape,encodeURI,encodeURIComponent编码结果相同。

最多使用的应为encodeURIComponent,它是将中文、韩文等特殊字符转换成utf-8格式的url编码,所以如果给后台传递参数需要使用encodeURIComponent时需要后台解码对utf-8支持(form中的编码方式和当前页面编码方式相同)

escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z

encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a- z,A-Z

encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z

javaScript中URL编码转换,escape() encodeURI() encodeURIComponent
2007年05月12日 星期六 下午 04:48

 在使用url进行参数传递时,经常会传递一些中文名的参数或URL地址,在后台处理时会发生转换错误。在有些传递页面使用GB2312,而在接收页面使用UTF8,这样接收到的参数就可能会与原来发生不一致。使用服务器端的urlEncode函数编码的URL,与使用客户端javascript的encodeURI函数编码的URL,结果就不一样。

javaScript中的编码方法:

escape() 方法:
采用ISO Latin字符集对指定的字符串进行编码。所有的空格符、标点符号、特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编码的16进制数字)。比如,空格符对应的编码是%20。unescape方法与此相反。不会被此方法编码的字符: @ * / +

encodeURI() 方法:把URI字符串采用UTF-8编码格式转化成escape格式的字符串。不会被此方法编码的字符:! @ # $& * ( ) = : / ; ? + '

encodeURIComponent() 方法:把URI字符串采用UTF-8编码格式转化成escape格式的字符串。与encodeURI()相比,这个方法将对更多的字符进行编码,比如 / 等字符。所以如果字符串里面包含了URI的几个部分的话,不能用这个方法来进行编码,否则 / 字符被编码之后URL将显示错误。不会被此方法编码的字符:! * ( )

因此,对于中文字符串来说,如果不希望把字符串编码格式转化成UTF-8格式的(比如原页面和目标页面的charset是一致的时候),只需要使用escape。如果你的页面是GB2312或者其他的编码,而接受参数的页面是UTF-8编码的,就要采用 encodeURI或者encodeURIComponent。

另外,encodeURI/encodeURIComponent是在javascript1.5之后引进的,escape则在 javascript1.0版本就有。

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() 不编码的字符: ~!@#$&*()=:/,;?+''

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

js 编码解码 escape,encodeURI,encodeURIComponent相关推荐

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

    转载地址:http://www.haorooms.com/post/js_escape_encodeURIComponent 引子 浏览器URl地址,上网一定会用到,但是浏览器地址有中文或者浏览器ur ...

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

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

  3. JS的编码:escape,encodeURI,encodeURIComponent,解码:unescape,decodeURI,decodeURIComp

    1:参数 传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断. 例如:<script language="javascript" ...

  4. js 文字转码 escape,encodeURI,encodeURIComponent(marksheng)

    js对文字进行转码的3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent 1. ...

  5. [转]js escape,encodeURI,encodeURIComponent

    js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent 1 ...

  6. JS中URL中的特殊字符问题:escape,encodeURI,encodeURIComponent(转)

    在使用url进行参数传递时,经常会传递一些中文名(或含有特殊字符)的参数或URL地址,在后台处理时会发生转换错误.在有些传递页面使用GB2312,而在接收页面使用UTF8,这样接收到的参数就可能会与原 ...

  7. URL转码escape() encodeURI() encodeURIComponent()

    转:http://deony2jacob1314.iteye.com/blog/1753068 js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent, ...

  8. escape,encodeURI,encodeURIComponent有什么区别?

    一.escape escape是对字符串进行编码,使得可以在所有的电脑上可读,但是encodeURI和encodeURIComponent是对URL进行编码的.escape和后两者几乎没关系. esc ...

  9. js编码解码一个蛮有意思的事

    前几天搞项目的时候,因为将页面基本全部html化了,传参数也就懒得进后台了,直接前台传参数,js解析吧 function getQueryString(name) {var reg = new Reg ...

最新文章

  1. 记一次升级Oracle驱动引发的死锁
  2. 程序员面试题精选100题(53)-C++/C#面试题(2)
  3. 【主席树】可持久化数组(金牌导航 可持久化数据结构-3)
  4. LeetCode 第 187 场周赛(1336/3107,前43.0%)
  5. 不需要安全实验证也可以开微信号_热门行业:电子专用设备工程师证考试报名时间及报名须知...
  6. zepto为什么不支持animate,报animate is not a function
  7. 数据库原理—关系模型的数据操作(八)
  8. js中改变原数组的方法以及解决改变原数组的方法
  9. mqtt等待来自服务器响应超时,等待来自服务器的响应时超时 (32000) at org.eclipse.paho.client.mqttv3.interna...
  10. Paraview使用指南
  11. GetWindowRect,GetClientRect,ScreenToClient MoveWindow SetWindowPos 用法说明
  12. 科普下Tier1,Tier2,Tier3,Tier4 T1, T2, T3, T4
  13. 用PS制作透明背景的电子签名
  14. Win7系统文件缺失怎么修复
  15. swagger ui 怎么输入对象_java swagger ui 添加header请求头参数的方法
  16. 基于历史对比学习的时序知识图谱推理
  17. VR是TAA的终结者吗?
  18. 远程控制软件开发_软件开发人员的远程工作
  19. android-使用asm.jar将Android手机屏幕投影到电脑
  20. 千氪公开课第一期|如何实现写作收益的最大化?-千氪

热门文章

  1. ps高斯模糊出现白边
  2. Python喜马拉雅有声小说音频文件爬虫
  3. 客户流失预测_如何不预测和防止客户流失
  4. 微信公众号开发本地环境搭建
  5. html用超链接将网页组织在一起,Javaweb-html
  6. 怎么把英文文献转译为中文?
  7. android 设置路由器,安卓手机怎么设置路由器?
  8. alios下载_AliOS Studio开源工具|AliOS Cloud App集成开发环境(AliOS Studio)下载 v1.2.1 官方Windows版 - 比克尔下载...
  9. 荟研新材料 毕克BYK420 水性涂料和颜料浓缩浆用液态流变助剂 抗流挂剂
  10. 精度检验方法(之二分类)