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 相关文章推荐
IE 缓存策略的BUG的解决方法
Jul 21 Javascript
Bootstrap表单布局样式源代码
Jul 04 Javascript
D3.js实现饼状图的方法详解
Sep 21 Javascript
JavaScript实现DOM对象选择器
Sep 24 Javascript
微信小程序 scroll-view组件实现列表页实例代码
Dec 14 Javascript
JS实现根据密码长度显示安全条功能
Mar 08 Javascript
Vue2.x中的Render函数详解
May 30 Javascript
浅析java线程中断的办法
Jul 29 Javascript
JS实现把一个页面层数据传递到另一个页面的两种方式
Aug 13 Javascript
vue通过滚动行为实现从列表到详情,返回列表原位置的方法
Aug 31 Javascript
Vue登录拦截 登录后继续跳转指定页面的操作
Aug 04 Javascript
Vue中使用Echarts仪表盘展示实时数据的实现
Nov 01 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
laravel使用Faker数据填充的实现方法
2019/04/12 PHP
不要小看注释掉的JS 引起的安全问题
2008/12/27 Javascript
在新窗口打开超链接的方法小结
2013/04/14 Javascript
Javascript小技巧之生成html元素
2014/05/15 Javascript
深入探讨JavaScript String对象
2015/03/09 Javascript
JavaScript中setUTCMilliseconds()方法的使用详解
2015/06/12 Javascript
jquery利用拖拽方式在图片上添加热链接
2015/11/24 Javascript
Javascript将双字节字符转换成单字节字符并计算长度
2016/06/22 Javascript
js实现多图左右切换功能
2016/08/04 Javascript
jquery easyui DataGrid简单示例
2017/01/23 Javascript
Bootstrap入门教程一Hello Bootstrap初识
2017/03/02 Javascript
easyui combogrid实现本地模糊搜索过滤多列
2017/05/13 Javascript
js禁止浏览器页面后退功能的实例(推荐)
2017/09/01 Javascript
实现一个完整的Node.js RESTful API的示例
2017/09/29 Javascript
node简单实现一个更改头像功能的示例
2017/12/29 Javascript
vue组件实践之可搜索下拉框功能
2018/11/25 Javascript
nodejs读取图片返回给浏览器显示
2019/07/25 NodeJs
jQuery实现数字华容道小游戏(实例代码)
2020/01/16 jQuery
vue 解决IOS10低版本白屏的问题
2020/11/17 Javascript
[01:12](回顾)DOTA2国际邀请赛,全世界DOTAer的盛宴
2014/07/01 DOTA
python实现朴素贝叶斯分类器
2018/03/28 Python
浅谈Python 递归算法指归
2019/08/22 Python
HTML5拍照和摄像机功能实战详解
2019/01/24 HTML / CSS
英国人最爱的饰品网站:Accessorize
2016/08/22 全球购物
挪威手表购物网站:Klokker
2016/09/19 全球购物
应届生会计电算化求职信
2013/10/03 职场文书
怎样有效的进行自我评价
2013/10/06 职场文书
新员工培训个人的自我评价
2013/10/09 职场文书
运动会广播稿80字
2014/01/23 职场文书
退休感言
2014/01/28 职场文书
函授本科自我鉴定
2014/02/04 职场文书
2014年幼儿园元旦活动方案
2014/02/13 职场文书
银行先进个人事迹材料
2014/05/11 职场文书
证券区域经理岗位职责
2015/04/10 职场文书
给领导敬酒词
2015/08/12 职场文书
pytorch加载预训练模型与自己模型不匹配的解决方案
2021/05/13 Python