URL编码转换,escape() encodeURI() encodeURIComponent()


Posted in Javascript onDecember 27, 2006

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

英文解释: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."
Edge 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.

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

英文解释: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. Edge 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

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

英文解释: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. 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格式的(比如原页面和目标页面的charset是一致的时候),只需要使用escape。如果你的页面是GB2312或者其他的编码,而接受参数的页面是UTF-8编码的,就要采用encodeURI或者encodeURIComponent。
      另外,encodeURI/encodeURIComponent是在javascript1.5之后引进的,escape则在javascript1.0版本就有。

英文注释:The escape() method does not encode the + character which is interpreted as a space on the server side as well as generated by forms with spaces in their fields. Due to this shortcoming, you should avoid use of escape() whenever possible. The best alternative is usually encodeURIComponent().Use of the encodeURI() method is a bit more specialized than escape() in that it encodes for URIs [REF] as opposed to the querystring, which is part of a URL. Use this method when you need to encode a string to be used for any resource that uses URIs and needs certain characters to remain un-encoded. Note that this method does not encode the ' character, as it is a valid character within URIs.Lastly, the encodeURIComponent() method should be used in most cases when encoding a single component of a URI. This method will encode certain chars that would normally be recognized as special chars for URIs so that many components may be included. Note that this method does not encode the ' character, as it is a valid character within URIs.

Javascript 相关文章推荐
javascript编程起步(第二课)
Jan 10 Javascript
File, FileReader 和 Ajax 文件上传实例分析(php)
Apr 27 Javascript
基于JavaScript 类的使用详解
May 07 Javascript
js实现网页自动刷新可制作节日倒计时效果
May 27 Javascript
js获取页面description的方法
May 21 Javascript
javascript HTML+CSS实现经典橙色导航菜单
Feb 16 Javascript
Boostrap模态窗口的学习小结
Mar 28 Javascript
详解vuejs几种不同组件(页面)间传值的方式
Jun 01 Javascript
jQuery动态添加元素无法触发绑定事件的解决方法分析
Jan 02 jQuery
基于openlayers4实现点的扩散效果
Aug 17 Javascript
angular6.x中ngTemplateOutlet指令的使用示例
Aug 09 Javascript
js不常见操作运算符总结
Nov 20 Javascript
escape、encodeURI、encodeURIComponent等方法的区别比较
Dec 27 #Javascript
破除网页鼠标右键被禁用的绝招大全
Dec 27 #Javascript
Windows Live的@live.com域名注册漏洞 利用代码
Dec 27 #Javascript
用javascript实现无刷新更新数据的详细步骤 asp
Dec 26 #Javascript
提高 DHTML 页面性能
Dec 25 #Javascript
js中几种去掉字串左右空格的方法
Dec 25 #Javascript
js静态作用域的功能。
Dec 25 #Javascript
You might like
php使用ereg验证文件上传的方法
2014/12/16 PHP
PHP中的数组处理函数实例总结
2016/01/09 PHP
PHP封装的MSSql操作类完整实例
2016/05/26 PHP
php大小写转换函数(strtolower、strtoupper)用法介绍
2017/11/17 PHP
PHP数组与字符串互相转换实例
2020/05/05 PHP
PHP 实现base64编码文件上传出现问题详解
2020/09/01 PHP
IE8 原生JSON支持
2009/04/13 Javascript
jquery 学习之二 属性相关
2010/11/23 Javascript
js控制input框只读实现示例
2014/01/20 Javascript
node.js中的console.warn方法使用说明
2014/12/09 Javascript
JS限制文本框只能输入数字和字母方法
2015/02/28 Javascript
ubuntu下安装nodejs以及升级的办法
2015/05/08 NodeJs
关于Jquery中的bind(),on()绑定事件方式总结
2016/10/26 Javascript
基于rem的移动端响应式适配方案(详解)
2017/07/07 Javascript
JS从非数组对象转数组的方法小结
2018/03/26 Javascript
vuejs 切换导航条高亮(路由菜单高亮)的方法示例
2018/05/29 Javascript
element-ui upload组件多文件上传的示例代码
2018/10/17 Javascript
解决Vue开发中对话框被遮罩层挡住的问题
2018/11/26 Javascript
微信小程序实现侧边栏分类
2019/10/21 Javascript
Vue.directive 实现元素scroll逻辑复用
2019/11/29 Javascript
vue+ts下对axios的封装实现
2020/02/18 Javascript
python中关于日期时间处理的问答集锦
2013/03/08 Python
python获取糗百图片代码实例
2013/12/18 Python
python使用MQTT给硬件传输图片的实现方法
2019/05/05 Python
pygame实现俄罗斯方块游戏(AI篇2)
2019/10/29 Python
CentOS7下安装python3.6.8的教程详解
2020/01/03 Python
postman和python mock测试过程图解
2020/02/22 Python
Pandas之缺失数据的实现
2021/01/06 Python
台湾乐天市场:日本No.1的网路购物网站
2017/03/22 全球购物
绘画设计学生的个人自我评价
2013/09/20 职场文书
银行会计业务的个人自我评价
2013/11/02 职场文书
找工作最新求职信
2013/12/22 职场文书
学校师德承诺书
2014/05/23 职场文书
求职信模板
2014/05/23 职场文书
考研导师推荐信范文
2015/03/27 职场文书
导游词之张家口
2019/12/13 职场文书