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 相关文章推荐
input 输入框获得/失去焦点时隐藏/显示文字(jquery版)
Apr 02 Javascript
Javascript中this的用法详解
Sep 22 Javascript
Node.js 学习笔记之简介、安装及配置
Mar 03 Javascript
jquery插件Jplayer使用方法简析
Apr 22 Javascript
JS获取及验证开始结束日期的方法
Aug 20 Javascript
vue.js图片转Base64上传图片并预览的实现方法
Aug 02 Javascript
Vue入门之数量加减运算操作示例
Dec 11 Javascript
react配置antd按需加载的使用
Feb 11 Javascript
微信小程序 授权登录详解(附完整源码)
Aug 23 Javascript
layer.open 子页面弹出层向父页面传输数据的例子
Sep 26 Javascript
vux-scroller实现移动端上拉加载功能过程解析
Oct 08 Javascript
jquery实现掷骰子小游戏
Oct 24 jQuery
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通用检测函数集合
2011/02/08 PHP
thinkphp跨库操作的简单代码实例
2016/09/22 PHP
PHP判断数组是否为空的常用方法(五种方法)
2017/02/08 PHP
解决php写入数据库乱码的问题
2019/09/17 PHP
jquery checkbox,radio是否选中的判断代码
2010/03/20 Javascript
纯JS实现五子棋游戏兼容各浏览器(附源码)
2013/04/24 Javascript
jQuery中对未来的元素绑定事件用bind、live or on
2014/04/17 Javascript
JavaScript通过字典进行字符串翻译转换的方法
2015/03/19 Javascript
浅析JS动态创建元素【两种方法】
2016/04/20 Javascript
js 获取范围内的随机数实例代码
2016/08/02 Javascript
AngularJS入门教程之双向绑定详解
2016/08/18 Javascript
JavaScript 限制文本框不可输入英文单双引号的方法
2016/12/20 Javascript
基于jPlayer三分屏的制作方法
2016/12/21 Javascript
微信小程序 tabs选项卡效果的实现
2017/01/05 Javascript
基于jquery实现左右上下移动效果
2018/05/02 jQuery
vue2.0 + ele的循环表单及验证字段方法
2018/09/18 Javascript
如何使用JavaScript实现栈与队列
2019/06/24 Javascript
关于layui 下拉列表的change事件详解
2019/09/20 Javascript
vue穿梭框实现上下移动
2021/01/29 Vue.js
Python命令行参数解析模块getopt使用实例
2015/04/13 Python
Python实现文件按照日期命名的方法
2015/07/09 Python
对python中使用requests模块参数编码的不同处理方法
2018/05/18 Python
Python读取mat文件,并转为csv文件的实例
2018/07/04 Python
Python函数和模块的使用总结
2019/05/20 Python
解决安装pyqt5之后无法打开spyder的问题
2019/12/13 Python
pandas中的数据去重处理的实现方法
2020/02/10 Python
Python如何脚本过滤文件中的注释
2020/05/27 Python
python线程池如何使用
2020/05/28 Python
基于HTML5 的人脸识别活体认证的实现方法
2016/06/22 HTML / CSS
美国滑雪和滑雪板商店:Buckman
2018/03/03 全球购物
爱尔兰最大的体育零售商:Life Style Sports
2019/06/12 全球购物
以实惠的价格轻松租车,免费取消:Easyrentcars
2019/07/16 全球购物
销售代表求职自荐信
2013/10/01 职场文书
电大毕业生自我鉴定
2014/04/10 职场文书
2014年社区个人工作总结
2014/12/02 职场文书
python3操作redis实现List列表实例
2021/08/04 Python