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 ajax 用户无刷新登录方法详解
Apr 28 Javascript
在Firefox下js select标签点击无法弹出
Mar 06 Javascript
JS实现样式清新的横排下拉菜单效果
Oct 09 Javascript
jquery实现删除一个元素后面的所有元素功能
Dec 21 Javascript
浅析JS动态创建元素【两种方法】
Apr 20 Javascript
Angularjs实现分页和分页算法的示例代码
Dec 23 Javascript
利用vue + koa2 + mockjs模拟数据的方法教程
Nov 22 Javascript
jquery 给动态生成的标签绑定事件的几种方法总结
Feb 24 jQuery
微信小程序基础教程之worker线程的使用方法
Jul 15 Javascript
vue从零实现一个消息通知组件的方法详解
Mar 16 Javascript
详解在IDEA中将Echarts引入web两种方式(使用js文件和maven的依赖导入)
Jul 11 Javascript
微信小程序 根据不同用户切换不同TabBar
Apr 21 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
Extended CHM PHP 语法手册之 DIY
2006/10/09 PHP
在服务端进行目录建立、删除,文件上传、删除的过程的php代码
2008/09/10 PHP
解析php中mysql_connect与mysql_pconncet的区别详解
2013/05/15 PHP
在WordPress中实现评论头像的自定义默认和延迟加载
2015/11/24 PHP
Laravel 加载第三方类库的方法
2018/04/20 PHP
高亮显示web页表格行的javascript代码
2010/11/19 Javascript
js对象与打印对象分析比较
2013/04/23 Javascript
JavaScript子类用Object.getPrototypeOf去调用父类方法解析
2013/12/05 Javascript
JavaScript中的getTime()方法使用详解
2015/06/10 Javascript
JavaScript数组的定义及数字操作技巧
2016/06/06 Javascript
js 监控iframe URL的变化实例代码
2017/07/12 Javascript
详解ES6之用let声明变量以及let loop机制
2017/07/15 Javascript
通过vue-cli来学习修改Webpack多环境配置和发布问题
2017/12/22 Javascript
详解在微信小程序的JS脚本中使用Promise来优化函数处理
2019/03/06 Javascript
vue+iview/elementUi实现城市多选
2019/03/28 Javascript
egg.js的基本使用和调用数据库的方法示例
2019/05/18 Javascript
jQuery实现的分页插件完整示例
2020/05/26 jQuery
vue实现日历表格(element-ui)
2020/09/24 Javascript
微信小程序实现可长按移动控件
2020/11/01 Javascript
vue 使用rules对表单字段进行校验的步骤
2020/12/25 Vue.js
Nodejs 微信小程序消息推送的实现
2021/01/20 NodeJs
[59:15]EG vs LGD 2018国际邀请赛淘汰赛BO3 第一场 8.26
2018/08/29 DOTA
python服务器与android客户端socket通信实例
2014/11/12 Python
python使用PyGame播放Midi和Mp3文件的方法
2015/04/24 Python
详解Python使用simplejson模块解析JSON的方法
2016/03/24 Python
使用TensorFlow搭建一个全连接神经网络教程
2020/02/06 Python
基于Python绘制美观动态圆环图、饼图
2020/06/03 Python
python打包生成so文件的实现
2020/10/30 Python
中国双语服务优势的在线购票及活动平台:247tickets
2018/10/26 全球购物
西班牙Polo衫品牌:Polo Club
2020/08/09 全球购物
职工代表大会主持词
2014/04/01 职场文书
学生乘坐校车安全责任书
2015/05/11 职场文书
2015年校务公开工作总结
2015/05/26 职场文书
CSS3 制作的图片滚动效果
2021/04/14 HTML / CSS
Python爬虫基础之爬虫的分类知识总结
2021/05/13 Python
gateway网关接口请求的校验方式
2021/07/15 Java/Android