escape(), encodeURI()和encodeURIComponent()(转)
escape(), encodeURI()和encodeURIComponent()是在Javascript中用于编码字符串的三个常用的方法,而他们之间的异同却困扰了很多的Javascript初学者,在这里对这三个方法详细地分析与比较一下。 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 " ." 译:escape方法以Unicode格式返回一个包含传入参数内容的string类型的值。 Escape方法会将传入参数中所有的空格、标点符号、重音字符以及其它任何非ASCII字符替换为%xx的编码形式,其中xx与其所表示的字符的16进制数表示形式相同。如空格字符的16进制表示形式为0x20,则此时xx应为20,即escape(‘ ’) 返回“ ”。 Mozilla Developer Core Javascript Guide中如是说: The escape and functions let you encode and decode strings. The escape function returns the hexadecimal encoding of an argument in the ISO Latin character set. The function returns the ASCII string for the specified hexadecimal encoding value. 译:escape和方法能够帮助你编码和解码字符串。escape方法对于ISO Latin字符集中的字符组成的参数,返回其16进制编码。相对应的,方法则能将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。如 ~!@#$%^&*(){}[]=:/,;?+\''"\\ 将被替换为 ~!@#$%^&*(){}[]=:/,;?+''"\ 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 certaincharacters 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方法,使用encodeURI方法会显得更专业一些。当你需要编码一整个URI的时候,你可以使用此方法,因为URI中的合法字符都不会被编码转换。需要注意到是字符’也是URI中的合法字符,所以也不会被编码转换。 encodeURIComponent方法在编码单个URIComponent(指请求参数)应当是最常用的。需要注意到是字符’也是URI中的合法字符,所以也不会被编码转换。 |
escape(), encodeURI()和encodeURIComponent()(转)的更多相关文章
- JS中 escape, encodeURI 和 encodeURIComponent的区别
为避免Url字符串在传递过程中的乱码,我们一般需要对字符串进行处理. 在Javascript中实现此功能的全局对象有3个,分别是:escape(), encodeURI() 和 encodeURI ...
- Javascript中escape(), encodeURI()和encodeURIComponent()之精析与比较
escape(), encodeURI()和encodeURIComponent()是在Javascript中用于编码字符串的三个常用的方法,而他们之间的异同却困扰了很多的Javascript初学者, ...
- escape encodeURI和encodeURIComponent的区别
escape(与之对应->unescape) escape是对字符串(string)进行编码(而另外两种是对URL),作用是让它们在所有电脑上可读.编码之后的效果是%XX或者%uXXXX这种形式 ...
- 使用escape、encodeURI 和 encodeURIComponent 解决url中文乱码问题
escape(), encodeURI()和encodeURIComponent()是在Javascript中用于编码字符串的三个常用的方法,而他们之间的异同却困扰了很多的Javascript初学者, ...
- 简单明了区分escape、encodeURI和encodeURIComponent
一.前言 讲这3个方法区别的文章太多了,但是大部分写的都很绕.本文试图从实践角度去讲这3个方法. 二.escape和它们不是同一类 简单来说,escape是对字符串(string)进行编码(而另外两种 ...
- 关于 escape、encodeURI、encodeURIComponent
参考资料:http://hi.baidu.com/flondon/item/983b3af35b83fa13ce9f3291 http://www.w3school.com.cn/js/jsref ...
- javascript中escape()、unescape()、encodeURI()、encodeURIComponent()、decodeURI()、decodeURIComponent()比较
这些URI方法encodeURI.encodeURIComponent().decodeURI().decodeURIComponent()代替了BOM的escape()和unescape()方法.U ...
- js中的三个编码函数:escape,encodeURI,encodeURIComponent
1. eacape(): 该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: * @ - _ + . / .其他所有的字符都会被转义序列替换.其它情况下es ...
- js 中escape,encodeURI,encodeURIComponent的区别
escape:方法不能能够用来对统一资源(URI)进行编码,对其编码应使用encodeURI和encodeURIComponent encodeURI:encodeURI ()方法返回一个编码的 UR ...
随机推荐
- CSS的Hack技术
主要是用来解决兼容性的特殊方法: IE都能识别*;标准浏览器(如FF)不能识别*: IE6能识别*,但不能识别 !important, IE7能识别*,也能识别!important,还有# FF不能识 ...
- Linux下面桌面的安装
挂载光盘后配置yum文件,配置后 使用yum grouplist 命令 查看组 用yum install '"GNOM桌面组 输入 init 5 即可进入桌面模式 0 关机 3命令行级 ...
- CSS常用属性
//边界线 border: 1px solid #E4E4E4; //绝对 定位 position: absolute; //相对定位 position: relative; //超出部分隐藏 ove ...
- Linux中总线设备驱动模型及平台设备驱动实例
本文将简要地介绍Linux总线设备驱动模型及其实现方式,并不会过多地涉及其在内核中的具体实现,最后,本文将会以平台总线为例介绍设备和驱动程序的实现过程. 目录: 一.总线设备驱动模型总体介绍及其实现方 ...
- php获取html纯文本,解决编辑器手动键入空格造成的无意义空白字符(空值问题)
在项目中,我们常常需要用到一些验证,不管是前台还是后台的,上传的问题时,需要内容不为空,但可视化编辑器的介入让手动敲入空格跳出了常规的检测.空格是一种排版的手段,但毫无内容只有空格就显得没有意义了,今 ...
- PHP的流程控制结构
1.break 使用break语句可以将深埋在嵌套循环中的语句退出到指定层数或直接退出到最外层,break是接受一个可选的数字参数来决定跳出几重语句.break可以跳出几重语句.break可以跳出几重 ...
- java 启动 shell脚本
run.sh p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "PingFang SC"; color: #000000 ...
- 如何给你的LinuxVPS装个远程桌面
有些人抱怨Linux的系统没有桌面,用着实在是不方便.今天整理一下网上的教程,给VPS装个桌面环境.Centos系统+X Window+GNOME Desktop. 此程序会安装无数乱七八糟的东西到你 ...
- SQL常用字段类型
中文常用字段类型 1. 名称类 nvarchar(10) 2. 数量 int 3. 时间 date 4. 货币 money 5. 编号 ...
- NotePad++左侧导航
NotePad++ 1. 增加左侧导航 a. 通过工具栏里面的“插件”->Plugin Manager-> Shwo Plugin Manager b. 找到Explorer勾选,点击I ...