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 相关文章推荐
jQuery 动态云标签插件
Nov 11 Javascript
快速解决jquery.touchSwipe左右滑动和垂直滚动条冲突
Apr 15 Javascript
tablesorter.js表格排序使用方法(支持中文排序)
Feb 10 Javascript
一次围绕setTimeout的前端面试经验分享
Jun 15 Javascript
Angular使用Md5加密的解决方法
Sep 16 Javascript
js解决软键盘遮挡输入框的问题分享
Dec 19 Javascript
微信小程序实现商品属性联动选择
Feb 15 Javascript
详解Vue+Element的动态表单,动态表格(后端发送配置,前端动态生成)
Apr 20 Javascript
layui form表单提交后实现自动刷新
Oct 25 Javascript
vue学习笔记之Vue中css动画原理简单示例
Feb 29 Javascript
JS apply用法总结和使用场景实例分析
Mar 14 Javascript
vue通过v-html指令渲染的富文本无法修改样式的解决方案
May 20 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利用hash冲突漏洞进行DDoS攻击的方法分析
2015/03/26 PHP
php类的定义与继承用法实例
2015/07/07 PHP
js之WEB开发调试利器:Firebug 下载
2007/01/13 Javascript
JavaScript 学习笔记(四)
2009/12/31 Javascript
jquery 插件学习(五)
2012/08/06 Javascript
Javascript图像处理—为矩阵添加常用方法
2012/12/27 Javascript
Jquery结合HTML5实现文件上传
2015/06/25 Javascript
jquery实现全选和全不选功能效果的实现代码【推荐】
2016/05/05 Javascript
easyUI实现(alert)提示框自动关闭的实例代码
2016/11/07 Javascript
ES6中的rest参数与扩展运算符详解
2017/07/18 Javascript
详解vue 实例方法和数据
2017/10/23 Javascript
vue的无缝滚动组件vue-seamless-scroll实例
2017/12/18 Javascript
彻底揭秘keep-alive原理(小结)
2019/05/05 Javascript
python将xml xsl文件生成html文件存储示例讲解
2013/12/03 Python
Python基于scrapy采集数据时使用代理服务器的方法
2015/04/16 Python
使用Python的Scrapy框架编写web爬虫的简单示例
2015/04/17 Python
Python 爬虫模拟登陆知乎
2016/09/23 Python
Python编程之变量赋值操作实例分析
2017/07/24 Python
python并发2之使用asyncio处理并发
2017/12/21 Python
PyQt5每天必学之布局管理
2018/04/19 Python
python 通过字符串调用对象属性或方法的实例讲解
2018/04/21 Python
python调用OpenCV实现人脸识别功能
2018/05/25 Python
Python命名空间的本质和加载顺序
2018/12/17 Python
python3 dict ndarray 存成json,并保留原数据精度的实例
2019/12/06 Python
django商品分类及商品数据建模实例详解
2020/01/03 Python
Python守护进程实现过程详解
2020/02/10 Python
Python print不能立即打印的解决方式
2020/02/19 Python
Pycharm修改python路径过程图解
2020/05/22 Python
Python爬虫入门教程02之笔趣阁小说爬取
2021/01/24 Python
前端canvas水印快速制作(附完整代码)
2019/09/19 HTML / CSS
美国顶级防滑鞋:Shoes For Crews
2017/03/27 全球购物
幼师专业求职推荐信
2013/11/08 职场文书
幼儿园校车司机的岗位职责
2014/01/30 职场文书
2014年电教工作总结
2014/12/19 职场文书
关于职业道德的心得体会
2016/01/18 职场文书
详解Python类和对象内容
2021/06/22 Python