javascript URL编码和解码使用说明


Posted in Javascript onApril 12, 2010

在有些传递页面使用GB2312,而在接收页面使用 UTF8,这样接收到的参数就可能会与原来发生不一致。使用服务器端的urlEncode函数编码的URL,与使用客户端javascript的 encodeURI函数编码的URL,结果就不一样。
javaScript中的编码方法:
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.

1.编码处理函数
1) encodeURI 返回一个对URI字符串编码后的结果。URL是最常见的一种URI;
2) decodeURI 将一个已编码的URI字符串解码成最原始的字符串返回;
3) 举例: < Script language = " javascript " > 输出结果如下: encodeStr: http://www.amigoxie.com/index.jsp?name=%E9%98%BF%E8%9C%9C%E6%9E%9C decodeStr: http://www.amigoxie.com/index.jsp?name=xind
2. 数值处理函数
1) parseInt 将一个字符串指定的进制转换为一个整数,语法格式为: parseInt(numString, [radix]) 第一个参数是要进行转换的字符串,是介于2到36之间的数值,用于指定进行字符串转换时所用的进制。 举例如下: 输出结果如下: 默认情况下的结果:32:32;032:26;0x32:50 转为2进制的结果:32:NaN;032:0;0x32:0 转为8进制的结果:32:26;032:26;0x32:0 转为16进制的结果:32:50;032:50;0x32:50 11001010转换后的结果: 2进制:202;16进制:285216784 8进制:2359816;10进制:11001010 43abc转换后:43;abc43转换后:NaN;abc转换后:NaN
2) parseFloat方法 该方法将一个字符串转换成对应的小数。 eg. 输出结果如下: 4.11 5.1 3) isNaN方法 该方法用于检测前两个方法返回值是否为非数值型,如果是,返回true,否则,反回false

Javascript 相关文章推荐
JS定时器实例
Apr 17 Javascript
escape函数解决js中ajax传递中文出现乱码问题
Oct 30 Javascript
jQuery实现行文字链接提示效果的方法
Mar 10 Javascript
Javascript使用function创建类的两种方法(推荐)
Nov 19 Javascript
vue2.0开发实践总结之疑难篇
Dec 07 Javascript
vue.js vue-router如何实现无效路由(404)的友好提示
Dec 20 Javascript
vue中引用swiper轮播插件的教程详解
Aug 16 Javascript
移动端(微信等使用vConsole调试console的方法
Mar 05 Javascript
从0到1搭建Element的后台框架的方法步骤
Apr 10 Javascript
微信小程序云开发之使用云函数
May 17 Javascript
vue-property-decorator用法详解
Dec 12 Javascript
Ant design vue中的联动选择取消操作
Oct 31 Javascript
!DOCTYPE声明对JavaScript的影响分析
Apr 12 #Javascript
3Z版基于jquery的图片复选框(asp.net+jquery)
Apr 12 #Javascript
javascript cookies 设置、读取、删除实例代码
Apr 12 #Javascript
javascript cookies操作集合
Apr 12 #Javascript
javascript 数组学习资料收集
Apr 11 #Javascript
在UpdatePanel内jquery easyui效果失效的解决方法
Apr 11 #Javascript
JavaScript调用Activex控件的事件的实现方法
Apr 11 #Javascript
You might like
在PHP中养成7个面向对象的好习惯
2010/01/28 PHP
PHP clearstatcache()函数详解
2010/03/02 PHP
PHP防CC攻击实现代码
2011/12/29 PHP
解析thinkphp基本配置 convention.php
2013/06/18 PHP
php实现多城市切换特效
2015/08/09 PHP
MooTools 1.2介绍
2009/09/14 Javascript
js 居中漂浮广告
2010/03/21 Javascript
Js使用WScript.Shell对象执行.bat文件和cmd命令
2014/12/18 Javascript
JavaScript实现Iterator模式实例分析
2015/06/09 Javascript
13个PHP函数超实用
2015/10/21 Javascript
AngularJS 路由详解和简单实例
2016/07/28 Javascript
JS for...in 遍历语句用法实例分析
2016/08/24 Javascript
微信小程序 数据访问实例详解
2016/10/08 Javascript
vue2.0实现导航菜单切换效果
2017/05/08 Javascript
JavaScript中set与get方法用法示例
2018/08/15 Javascript
Vue-cli项目部署到Nginx服务器的方法
2019/11/01 Javascript
JavaScript实现Tab选项卡切换
2020/02/13 Javascript
python使用新浪微博api上传图片到微博示例
2014/01/10 Python
python optparse模块使用实例
2015/04/09 Python
使用Python3制作TCP端口扫描器
2017/04/17 Python
Python自定义装饰器原理与用法实例分析
2018/07/16 Python
python3 中文乱码与默认编码格式设定方法
2018/10/31 Python
对django layer弹窗组件的使用详解
2019/08/31 Python
解决Numpy中sum函数求和结果维度的问题
2019/12/06 Python
python 图像判断,清晰度(明暗),彩色与黑白实例
2020/06/04 Python
使用Python实现微信拍一拍功能的思路代码
2020/07/09 Python
使用PyCharm官方中文语言包汉化PyCharm
2020/11/18 Python
塔吉特百货公司官网:Target
2017/04/27 全球购物
北美领先的智能产品购物网站:Wellbots
2018/06/11 全球购物
货代行业个人求职简历的自我评价
2013/10/22 职场文书
建筑专业毕业生推荐信
2013/11/21 职场文书
新学期校长寄语
2014/01/18 职场文书
大一学生职业生涯规划
2014/03/11 职场文书
2015年党风建设工作总结
2015/04/29 职场文书
院系推荐意见
2015/06/05 职场文书
Django如何创作一个简单的最小程序
2021/05/12 Python