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 new Date() 出现NaN 的分析
Oct 23 Javascript
extjs中form与grid交互数据(record)的方法
Aug 29 Javascript
jquery创建表格(自动增加表格)代码分享
Dec 25 Javascript
再分享70+免费的jquery 图片滑块效果插件和教程
Dec 15 Javascript
在JavaScript中如何解决用execCommand(
Oct 19 Javascript
js简单实现图片延迟加载的方法
Jul 19 Javascript
深入理解Angular2 模板语法
Aug 07 Javascript
Java与JavaScript中判断两字符串是否相等的区别
Mar 13 Javascript
jQuery代码优化方法总结
Jan 29 jQuery
vue data引入本地图片的两种方式小结
Nov 13 Javascript
Vue+Element UI 树形控件整合下拉功能菜单(tree + dropdown +input)
Aug 28 Javascript
js将日期格式转换为YYYY-MM-DD HH:MM:SS
Sep 18 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
输出控制类
2006/10/09 PHP
php动态生成JavaScript代码
2009/03/09 PHP
php入门学习知识点六 PHP文件的读写操作代码
2011/07/14 PHP
PHP 读取Postgresql中的数组
2013/04/14 PHP
CI框架扩展系统核心类的方法分析
2016/05/23 PHP
搭建自己的PHP MVC框架详解
2017/08/16 PHP
两个select多选模式的选项相互移动(示例代码)
2014/01/11 Javascript
Javascript的&amp;&amp;和||的另类用法
2014/07/23 Javascript
使用JS画图之点、线、面
2015/01/12 Javascript
javaScript基础语法介绍
2015/02/28 Javascript
纯JavaScript实现的兼容各浏览器的添加和移除事件封装
2015/03/28 Javascript
如何利用Promises编写更优雅的JavaScript代码
2016/05/17 Javascript
js学习阶段总结(必看篇)
2016/06/16 Javascript
微信公众平台开发教程(六)获取个性二维码的实例
2016/12/02 Javascript
js实现自定义进度条效果
2017/03/15 Javascript
jQuery实现分页功能(含ajax请求、后台数据、附完整demo)
2017/04/03 jQuery
小程序:授权、登录、session_key、unionId的详解
2019/05/15 Javascript
vue项目中使用eslint+prettier规范与检查代码的方法
2020/01/16 Javascript
ES2020让代码更优美的运算符 (?.) (??)
2021/01/04 Javascript
CentOS下使用yum安装python-pip失败的完美解决方法
2017/08/16 Python
Python内置模块ConfigParser实现配置读写功能的方法
2018/02/12 Python
python json.loads兼容单引号数据的方法
2018/12/19 Python
python psutil模块使用方法解析
2019/08/01 Python
浅析matlab中imadjust函数
2020/02/27 Python
利用python画出AUC曲线的实例
2020/02/28 Python
matplotlib阶梯图的实现(step())
2021/03/02 Python
HTML5的结构和语义(5):内嵌媒体
2008/10/17 HTML / CSS
英国音乐设备和乐器商店:Gear4music
2017/10/16 全球购物
某/etc/fstab文件中的某行如下: /dev/had5 /mnt/dosdata msdos defaults,usrquota 1 2 请解释其含义
2013/04/11 面试题
市场营销求职信范文
2014/02/21 职场文书
一年级学生期末评语
2014/04/21 职场文书
团委书记的竞聘演讲稿
2014/04/24 职场文书
文体活动总结范文
2014/05/05 职场文书
体育教师求职信
2014/06/30 职场文书
2014年财务科工作总结
2014/11/11 职场文书
2015年组织部工作总结
2015/04/03 职场文书