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 折半查找字符在数组中的位置(有序列表)
Dec 09 Javascript
javascript页面渲染速度测试脚本分享
Apr 15 Javascript
JavaScript中exec函数用法实例分析
Jun 08 Javascript
JS基于Ajax实现的网页Loading效果代码
Oct 27 Javascript
JavaScript实现简单Tip提示框效果
Apr 20 Javascript
所见即所得的富文本编辑器bootstrap-wysiwyg使用方法详解
May 27 Javascript
Angularjs 创建可复用组件实例代码
Oct 09 Javascript
jQuery中Datatables增加跳转到指定页功能
Feb 08 Javascript
微信分享调用jssdk实例
Jun 08 Javascript
微信小程序实现笑脸评分功能
Nov 03 Javascript
VueJS 组件参数名命名与组件属性转化问题
Dec 03 Javascript
简单了解three.js 着色器材质
Aug 03 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 魔术函数使用说明
2010/02/21 PHP
php递归删除目录与文件的方法
2015/01/30 PHP
jQuery表单验证插件formValidator(改进版)
2012/02/03 Javascript
浅谈js 闭包引起的内存泄露问题
2015/06/22 Javascript
简单谈谈javascript Date类型
2015/09/06 Javascript
node.js连接mongoDB数据库 快速搭建自己的web服务
2016/04/17 Javascript
JavaScript 中有关数组对象的方法(详解)
2016/08/15 Javascript
JS实现复制内容到剪贴板功能
2017/02/05 Javascript
Ajax异步文件上传与NodeJS express服务端处理
2017/04/01 NodeJs
利用JS实现scroll自定义滚动效果详解
2017/10/17 Javascript
基于jQuery解决ios10以上版本缩放问题
2017/11/03 jQuery
JavaScript 数组去重并统计重复元素出现的次数实例
2017/12/14 Javascript
微信小程序如何获取用户收货地址
2018/11/27 Javascript
JavaScript检测浏览器是否支持CSS变量代码实例
2020/04/03 Javascript
JavaScript事件委托实现原理及优点进行
2020/08/29 Javascript
[01:10:27]DOTA2-DPC中国联赛正赛 SAG vs XG BO3 第二场 3月5日
2021/03/11 DOTA
Python 列表list使用介绍
2014/11/30 Python
python实现斐波那契数列的方法示例
2017/01/12 Python
python基于ID3思想的决策树
2018/01/03 Python
详解Python中的内建函数,可迭代对象,迭代器
2019/04/29 Python
Python的形参和实参使用方式
2019/12/24 Python
解决python 执行sql语句时所传参数含有单引号的问题
2020/06/06 Python
使用Python+Appuim 清理微信的方法
2021/01/26 Python
日本土著品牌,综合型购物网站:Cecile
2016/08/23 全球购物
日本最大级玩偶手办购物:あみあみ Amiami
2018/04/23 全球购物
Mybag美国/加拿大:英国奢华包包和名牌手袋网站
2020/02/16 全球购物
新浪微博实习心得体会
2014/01/27 职场文书
迟到检讨书300字
2014/02/14 职场文书
党的群众路线教育实践活动宣传方案
2014/02/23 职场文书
建筑管理专业求职信
2014/07/28 职场文书
2015财务年终工作总结范文
2015/05/22 职场文书
小学开学典礼新闻稿
2015/07/17 职场文书
护士爱岗敬业心得体会
2016/01/25 职场文书
2019优秀干部竞聘演讲稿范文!
2019/07/02 职场文书
Java反应式框架Reactor中的Mono和Flux
2021/07/25 Java/Android
我国拿下天问一号火星着陆区附近 22 个地理实体命名:平乐、西柏坡、古田、漠河等
2022/04/29 数码科技