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 相关文章推荐
广告切换效果(缓动切换)
May 27 Javascript
javascript获取网页中指定节点的父节点、子节点的方法小结
Apr 24 Javascript
javascript 实现字符串反转的三种方法
Nov 23 Javascript
JavaScript数组常用方法
Mar 02 Javascript
js实现简单折叠、展开菜单的方法
Aug 28 Javascript
JavaScript中数组的合并以及排序实现示例
Oct 24 Javascript
bootstrap3使用bootstrap datetimepicker日期插件
May 24 Javascript
Bootstrap Tooltip显示换行和左对齐的解决方案
Oct 11 Javascript
vue单文件组件lint error自动fix与styleLint报错自动fix详解
Jan 08 Javascript
微信小程序 setData 对 data数据影响问题
Apr 18 Javascript
Node.js安装详细步骤教程(Windows版)详解
Sep 01 Javascript
微信小程序swiper左右扩展各显示一半代码实例
Dec 05 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
火车头采集器3.0采集图文教程
2007/03/17 PHP
php eval函数用法总结
2012/10/31 PHP
关于PHP内存溢出问题的解决方法
2013/06/25 PHP
php自动给网址加上链接的方法
2015/06/02 PHP
PHP实现GIF图片验证码
2015/11/04 PHP
PHP+Ajax实现的无刷新分页功能详解【附demo源码下载】
2017/07/03 PHP
img onload事件绑定各浏览器均可执行
2012/12/19 Javascript
对frameset、frame、iframe的js操作示例代码
2013/08/16 Javascript
js弹出确认是否删除对话框
2014/03/27 Javascript
dreamweaver 8实现Jquery自动提示
2014/12/04 Javascript
JavaScript使用setTimeout实现延迟弹出警告框的方法
2015/04/07 Javascript
JSON与XML优缺点对比分析
2015/07/17 Javascript
用js动态添加html元素,以及属性的简单实例
2016/07/19 Javascript
Js遍历键值对形式对象或Map形式的方法
2016/08/08 Javascript
vue.js使用v-if实现显示与隐藏功能示例
2018/07/06 Javascript
浅谈Vue.js 中的 v-on 事件指令的使用
2018/11/25 Javascript
深入koa-bodyparser原理解析
2019/01/16 Javascript
基于vue的验证码组件的示例代码
2019/01/22 Javascript
微信小程序下拉加载和上拉刷新两种实现方法详解
2019/09/05 Javascript
原生js实现日历效果
2020/03/02 Javascript
JavaScript Array.flat()函数用法解析
2020/09/02 Javascript
浅析AST抽象语法树及Python代码实现
2016/06/06 Python
TensorFlow实现RNN循环神经网络
2018/02/28 Python
Python使用装饰器模拟用户登陆验证功能示例
2018/08/24 Python
Python基础之函数的定义与使用示例
2019/03/23 Python
使用pymysql查询数据库,把结果保存为列表并获取指定元素下标实例
2020/05/15 Python
学python需要去培训机构吗
2020/07/01 Python
python实现canny边缘检测
2020/09/14 Python
美国最好的葡萄酒网上商店:Wine Library
2019/11/02 全球购物
教师个人的自我评价分享
2014/01/02 职场文书
给医务人员表扬信
2014/01/12 职场文书
教师申诉制度
2014/01/29 职场文书
实习介绍信模板
2015/01/30 职场文书
工作岗位职责范本
2015/02/15 职场文书
安全生产感想
2015/08/07 职场文书
python字符串常规操作大全
2021/05/02 Python