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 相关文章推荐
双击滚屏-常用推荐
Nov 29 Javascript
jQuery动态修改超链接地址的方法
Feb 13 Javascript
AngularJS的内置过滤器详解
May 14 Javascript
jquery实现用户信息修改验证输入方法汇总
Jul 18 Javascript
javascript+HTML5 Canvas绘制转盘抽奖
May 16 Javascript
浅谈jquery高级方法描述与应用
Oct 04 Javascript
Node.js制作简单聊天室
Jan 12 Javascript
JavaScript无缝滚动效果的实例代码
Mar 27 Javascript
去掉vue 中的代码规范检测两种方法(Eslint验证)
Mar 21 Javascript
webpack 样式加载的实现原理
Jun 12 Javascript
javascript设计模式 ? 简单工厂模式原理与应用实例分析
Apr 09 Javascript
一篇文章了解正则表达式的替换技巧
Feb 24 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 8小时时间差的解决方法小结
2009/12/22 PHP
PHP防止跨域提交表单
2013/11/01 PHP
php格式化时间戳显示友好的时间实现思路及代码
2014/10/23 PHP
C# WinForm中实现快捷键自定义设置实例
2015/01/23 PHP
搭建PhpStorm+PhpStudy开发环境的超详细教程
2020/09/17 PHP
js replace正则表达式应用案例讲解
2013/01/17 Javascript
Jquery显示、隐藏元素以及添加删除样式
2013/08/09 Javascript
使用百度地图api实现根据地址查询经纬度
2014/12/11 Javascript
jQuery的实例及必知重要的jQuery选择器详解
2016/05/20 Javascript
vue获取当前点击的元素并传值的实例
2018/03/09 Javascript
vue实现tab切换外加样式切换方法
2018/03/16 Javascript
用vuex写了一个购物车H5页面的示例代码
2018/12/04 Javascript
微信小程序实现星级评价效果
2018/12/28 Javascript
微信小程序实现复选框效果
2018/12/28 Javascript
微信小程序生成海报分享朋友圈的实现方法
2019/05/06 Javascript
全面分析JavaScript 继承
2019/05/30 Javascript
layui数据表格重载实现往后台传参
2019/11/15 Javascript
JS正则表达式验证密码强度
2020/03/18 Javascript
WebPack工具运行原理及入门教程
2020/12/02 Javascript
[41:54]2018DOTA2亚洲邀请赛 4.1 小组赛A组加赛 TNC vs Liquid
2018/04/03 DOTA
Python+pandas计算数据相关系数的实例
2018/07/03 Python
使用python分析统计自己微信朋友的信息
2019/07/19 Python
详解pandas中MultiIndex和对象实际索引不一致问题
2019/07/23 Python
ubuntu 安装pyqt5和卸载pyQt5的方法
2020/03/24 Python
Python-jenkins 获取job构建信息方式
2020/05/12 Python
keras load model时出现Missing Layer错误的解决方式
2020/06/11 Python
canvas 绘图时位置偏离的问题解决
2020/09/16 HTML / CSS
瑞贝卡·明可弗包包官网:Rebecca Minkoff
2016/07/21 全球购物
Linux上比较文件的命令都有哪些
2012/02/24 面试题
高考自主招生自荐信
2013/10/20 职场文书
小学教师的个人自我鉴定
2013/10/26 职场文书
电信专业毕业生推荐信
2013/11/18 职场文书
2014年助理政工师工作总结
2014/12/19 职场文书
房地产销售主管岗位职责
2015/02/13 职场文书
广告文案的撰写技巧(实用干货)
2019/08/23 职场文书
matlab xlabel位置的设置方式
2021/05/21 Python