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 相关文章推荐
用js+xml自动生成表格的东西
Dec 21 Javascript
jquery实现带单选按钮的表格行选中时高亮显示
Aug 01 Javascript
jquery的live使用注意事项
Feb 18 Javascript
jquery 取子节点及当前节点属性值的方法
Aug 24 Javascript
javascript实现倒计时N秒后网页自动跳转代码
Dec 11 Javascript
jQuery实现鼠标划过添加和删除class的方法
Jun 26 Javascript
js实现动态显示时间效果
Mar 06 Javascript
JS中Safari浏览器中的Date
Jul 17 Javascript
angular动态删除ng-repaeat添加的dom节点的方法
Jul 20 Javascript
ES6的Fetch异步请求的实现方法
Dec 07 Javascript
详解vue 图片上传功能
Apr 30 Javascript
解决angular 使用原生拖拽页面卡顿及表单控件输入延迟问题
Apr 21 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
苏联队长,苏联超人蝙蝠侠,这些登场的“山寨”英雄真的很严肃
2020/04/09 欧美动漫
php将日期格式转换成xx天前的格式
2015/04/16 PHP
图文详解PHP环境搭建教程
2016/07/16 PHP
老生常谈PHP面向对象之命令模式(必看篇)
2017/05/24 PHP
JS 打印界面的CSS居中代码适用所有浏览器
2014/03/19 Javascript
Javascript遍历table中的元素示例代码
2014/07/08 Javascript
jQuery实现的指纹扫描效果实例(附演示与demo源码下载)
2016/01/26 Javascript
JavaScript编写页面半透明遮罩效果的简单示例
2016/05/09 Javascript
Bootstrap学习笔记之css样式设计(1)
2016/06/07 Javascript
nodejs之get/post请求的几种方式小结
2017/07/26 NodeJs
详解node child_process模块学习笔记
2018/01/24 Javascript
vue拦截器实现统一token,并兼容IE9验证功能
2018/04/26 Javascript
Vue函数式组件的应用实例详解
2019/08/30 Javascript
微信小程序实现多行文字超出部分省略号显示功能
2019/10/23 Javascript
JS实现百度搜索框关键字推荐
2020/02/17 Javascript
js实现超级玛丽小游戏
2020/03/18 Javascript
jQuery实现简单评论区功能
2020/10/26 jQuery
举例讲解Python设计模式编程的代理模式与抽象工厂模式
2016/01/16 Python
Python json 错误xx is not JSON serializable解决办法
2017/03/15 Python
Python自然语言处理之词干,词形与最大匹配算法代码详解
2017/11/16 Python
基于anaconda下强大的conda命令介绍
2018/06/11 Python
Python之列表的插入&替换修改方法
2018/06/28 Python
python利用requests库模拟post请求时json的使用教程
2018/12/07 Python
python paramiko利用sftp上传目录到远程的实例
2019/01/03 Python
Python实现的服务器示例小结【单进程、多进程、多线程、非阻塞式】
2019/05/23 Python
深入了解Python在HDA中的应用
2019/09/05 Python
详解mac python+selenium+Chrome 简单案例
2019/11/08 Python
tensorflow使用指定gpu的方法
2020/02/04 Python
生产经理的自我评价分享
2013/11/07 职场文书
拾金不昧的表扬信
2014/01/16 职场文书
家具公司总经理岗位职责
2014/07/08 职场文书
优秀团员事迹材料2000字
2014/08/20 职场文书
个人作风建设总结
2014/10/23 职场文书
CSS3 实现NES游戏机的示例代码
2021/04/21 HTML / CSS
MySQL数据库必备之条件查询语句
2021/10/15 MySQL
Python Pandas 删除列操作
2022/03/16 Python