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 相关文章推荐
SUN的《AJAX与J2EE》全文译了
Feb 23 Javascript
js判断undefined变量类型使用typeof
Jun 03 Javascript
Function.prototype.call.apply结合用法分析示例
Jul 03 Javascript
js中符号转意问题示例探讨
Aug 19 Javascript
node.js 开发指南 ? Node.js 连接 MySQL 并进行数据库操作
Jul 29 Javascript
JS鼠标拖拽实例分析
Nov 23 Javascript
原生JS下拉加载插件分享
Dec 26 Javascript
js原生代码实现轮播图的实例讲解
Jul 28 Javascript
vue项目中使用tinymce编辑器的步骤详解
Sep 11 Javascript
通过js示例讲解时间复杂度与空间复杂度
Aug 06 Javascript
如何基于filter实现网站整体变灰功能
Apr 17 Javascript
React 高阶组件HOC用法归纳
Jun 13 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
Windows下部署Apache+PHP+MySQL运行环境实战
2012/08/31 PHP
PHP实现的简单日历类
2014/11/29 PHP
thinkPHP中volist标签用法示例
2016/12/06 PHP
PHP面向对象之里氏替换原则简单示例
2018/04/08 PHP
用js实现预览待上传的本地图片
2007/03/15 Javascript
漂亮的widgets,支持换肤和后期开发新皮肤
2007/04/23 Javascript
javascript Excel操作知识点
2009/04/24 Javascript
JavaScript中的getMilliseconds()方法使用详解
2015/06/10 Javascript
微信开发 JS-SDK 6.0.2 经常遇到问题总结
2016/12/08 Javascript
React Native验证码倒计时工具类分享
2017/10/24 Javascript
简述Angular 5 快速入门
2017/11/04 Javascript
Node.js中,在cmd界面,进入退出Node.js运行环境的方法
2018/05/12 Javascript
Express本地测试HTTPS的示例代码
2018/06/06 Javascript
vue-router 源码之实现一个简单的 vue-router
2018/07/02 Javascript
React+Redux实现简单的待办事项列表ToDoList
2019/09/29 Javascript
vue prop属性传值与传引用示例
2019/11/13 Javascript
简单介绍Ruby中的CGI编程
2015/04/10 Python
Python的Flask框架中web表单的教程
2015/04/20 Python
Python中字符串对齐方法介绍
2015/05/21 Python
python学习之面向对象【入门初级篇】
2017/01/21 Python
python实现支付宝转账接口
2019/05/07 Python
Python进阶:生成器 懒人版本的迭代器详解
2019/06/29 Python
使用python 的matplotlib 画轨道实例
2020/01/19 Python
python如何调用字典的key
2020/05/25 Python
使用HTML5 Canvas绘制圆角矩形及相关的一些应用举例
2016/03/22 HTML / CSS
万得城电器土耳其网站:欧洲第一大电子产品零售商
2016/10/07 全球购物
Farfetch巴西官网:奢侈品牌时尚购物平台
2020/10/19 全球购物
俄罗斯极限运动网上商店:Board Shop №1
2020/12/18 全球购物
2014小学二年级班主任工作总结
2014/12/05 职场文书
病危通知书样本
2015/04/17 职场文书
小学生暑假生活总结
2015/07/13 职场文书
学校少先队工作总结
2015/08/12 职场文书
高中英语教学反思范文
2016/03/02 职场文书
【海涛解说】pis亲自推荐,其实你从来不会玩NW
2022/04/01 DOTA
速龙x4-860k处理器相当于i几
2022/04/20 数码科技
Python实现双向链表
2022/05/25 Python