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 相关文章推荐
Javascript 个人笔记(没有整理,很乱)
Jul 07 Javascript
用JS实现的一个include函数
Jul 21 Javascript
javascript html 静态页面传参数
Apr 10 Javascript
用jquery和json从后台获得数据集的代码
Nov 07 Javascript
Bootstrap导航条可点击和鼠标悬停显示下拉菜单的实现代码
Jun 23 Javascript
javascript学习笔记_浅谈基础语法,类型,变量
Sep 19 Javascript
简单实现bootstrap导航效果
Feb 07 Javascript
jQuery+Ajax实现用户名重名实时检测
Jun 01 jQuery
教你5分钟学会用requirejs(必看篇)
Jul 25 Javascript
JS+canvas动态绘制饼图的方法示例
Sep 12 Javascript
angular基于ng-alain定义自己的select组件示例
Feb 23 Javascript
React项目动态设置title标题的方法示例
Sep 26 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
DC这些乐高系列动画电影你看过几部?
2020/04/09 欧美动漫
php stripslashes和addslashes的区别
2014/02/03 PHP
php使用strip_tags()去除html标签仍有空白的解决方法
2016/07/28 PHP
PHP版微信第三方实现一键登录及获取用户信息的方法
2016/10/14 PHP
PHP框架实现WebSocket在线聊天通讯系统
2019/11/21 PHP
JavaScript 调试器简介
2009/02/21 Javascript
ASP.NET jQuery 实例8 (动态添加内容到DropDownList)
2012/02/03 Javascript
jquery制作弹窗提示窗口代码分享
2014/03/02 Javascript
单击某一段文字改写文本颜色
2014/06/06 Javascript
javascript正则表达式基础知识入门
2015/04/20 Javascript
CSS3实现动态背景登录框的代码
2015/07/28 Javascript
sencha ext js 6 快速入门(必看)
2016/06/01 Javascript
Angularjs使用过滤器完成排序功能
2017/09/20 Javascript
JS实现提交表单前的数字及邮箱校检功能
2017/11/13 Javascript
jQuery动态添加元素无法触发绑定事件的解决方法分析
2018/01/02 jQuery
利用js将ajax获取到的后台数据动态加载至网页中的方法
2018/08/08 Javascript
JavaScript事件发布/订阅模式原理与用法分析
2018/08/21 Javascript
Vue父组件如何获取子组件中的变量
2019/07/24 Javascript
解决vue bus.$emit触发第一次$on监听不到问题
2020/07/28 Javascript
梳理一下vue中的生命周期
2020/12/30 Vue.js
[42:22]DOTA2上海特级锦标赛C组小组赛#1 OG VS Archon第一局
2016/02/27 DOTA
[07:57]2018DOTA2国际邀请赛寻真——PSG.LGD凤凰浴火
2018/08/12 DOTA
Flask框架通过Flask_login实现用户登录功能示例
2018/07/17 Python
Python面向对象之私有属性和私有方法应用案例分析
2019/12/31 Python
细说NumPy数组的四种乘法的使用
2020/12/18 Python
CSS3实战第一波 让我们尽情的圆角吧
2010/08/27 HTML / CSS
Seavenger官网:潜水服、浮潜、靴子和袜子
2020/03/05 全球购物
C#笔试题集合
2013/06/21 面试题
小学生感恩父母演讲稿
2014/08/28 职场文书
2014光棍节大学生联谊活动方案
2014/10/10 职场文书
电影焦裕禄观后感
2015/06/09 职场文书
品德与社会教学反思
2016/02/24 职场文书
MySQL库表名大小写的选择
2021/06/05 MySQL
MySQL中B树索引和B+树索引的区别详解
2022/03/03 MySQL
《乙女游戏世界对路人角色很不友好》OP主题曲无字幕动画MV公开
2022/04/05 日漫
JavaScript前端面试扁平数据转tree与tree数据扁平化
2022/06/14 Javascript