javascript 三种编解码方式
作者:bea
1、 传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断。 例如:<script language="javascript">document.write('<a href="http://passport.baidu.com/?logout&aid=7&u='+encodeURIComponent("http://cang.baidu.com/bruce42")+'">退出</a&g
1、 传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断。
例如:<script language="javascript">document.write('<a href="http://passport.baidu.com/?logout&aid=7&u='+encodeURIComponent("http://cang.baidu.com/bruce42")+'">退出</a>');</script>
2、 进行url跳转时可以整体使用encodeURI
例如:Location.href=encodeURI("http://cang.baidu.com/do/s?word=百度&ct=21");
3、 js使用数据时可以使用escape[Huoho.Com编辑]
例如:搜藏中history纪录。
4、 escape对0-255以外的unicode值进行编码时输出%u****格式,其它情况下escape,encodeURI,encodeURIComponent编码结果相同。
最多使用的应为encodeURIComponent,它是将中文、韩文等特殊字符转换成utf-8格式的url编码,所以如果给后台传递参数需要使用encodeURIComponent时需要后台解码对utf-8支持(form中的编码方式和当前页面编码方式相同)
escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z
encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z
encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z
========================================================
unescape 方法
从用 escape 方法编码的 String 对象中返回已解码的字符串。
function unescape(charString : String) : String
参数
charString
必选。要解码的 String 对象或文本。
备注
unescape 方法返回一个包含 charstring 内容的字符串值。所有以 %xx 十六进制形式编码的字符都用 ASCII 字符集当中等效的字符代替。以 %uxxxx 格式(Unicode 字符)编码的字符用十六进制编码 xxxx 的 Unicode 字符代替。注意 unescape 方法不应用于解码“统一资源标识符”(URI)。请改用 decodeURI 和 decodeURIComponent 方法。
decodeURI 方法
返回一个已编码的统一资源标识符 (URI) 的非编码形式。
function decodeURI(URIstring : String) : String
参数
URIstring
必选。表示编码 URI 的字符串。
备注
使用 decodeURI 方法代替已经过时的 unescape 方法。
decodeURI 方法返回一个字符串值。
如果 URIString 无效,将发生 URIError。
decodeURIComponent 方法
返回统一资源标识符 (URI) 的一个已编码组件的非编码形式。
function decodeURIComponent(encodedURIString : String) : String
必选的 encodedURIString 参数是一个表示已编码的 URI 组件的值。
备注
URIComponent 是一个完整的 URI 的一部分
有用 | 无用
例如:<script language="javascript">document.write('<a href="http://passport.baidu.com/?logout&aid=7&u='+encodeURIComponent("http://cang.baidu.com/bruce42")+'">退出</a>');</script>
2、 进行url跳转时可以整体使用encodeURI
例如:Location.href=encodeURI("http://cang.baidu.com/do/s?word=百度&ct=21");
3、 js使用数据时可以使用escape[Huoho.Com编辑]
例如:搜藏中history纪录。
4、 escape对0-255以外的unicode值进行编码时输出%u****格式,其它情况下escape,encodeURI,encodeURIComponent编码结果相同。
最多使用的应为encodeURIComponent,它是将中文、韩文等特殊字符转换成utf-8格式的url编码,所以如果给后台传递参数需要使用encodeURIComponent时需要后台解码对utf-8支持(form中的编码方式和当前页面编码方式相同)
escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z
encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z
encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z
========================================================
unescape 方法
从用 escape 方法编码的 String 对象中返回已解码的字符串。
function unescape(charString : String) : String
参数
charString
必选。要解码的 String 对象或文本。
备注
unescape 方法返回一个包含 charstring 内容的字符串值。所有以 %xx 十六进制形式编码的字符都用 ASCII 字符集当中等效的字符代替。以 %uxxxx 格式(Unicode 字符)编码的字符用十六进制编码 xxxx 的 Unicode 字符代替。注意 unescape 方法不应用于解码“统一资源标识符”(URI)。请改用 decodeURI 和 decodeURIComponent 方法。
decodeURI 方法
返回一个已编码的统一资源标识符 (URI) 的非编码形式。
function decodeURI(URIstring : String) : String
参数
URIstring
必选。表示编码 URI 的字符串。
备注
使用 decodeURI 方法代替已经过时的 unescape 方法。
decodeURI 方法返回一个字符串值。
如果 URIString 无效,将发生 URIError。
decodeURIComponent 方法
返回统一资源标识符 (URI) 的一个已编码组件的非编码形式。
function decodeURIComponent(encodedURIString : String) : String
必选的 encodedURIString 参数是一个表示已编码的 URI 组件的值。
备注
URIComponent 是一个完整的 URI 的一部分
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- jquery 图片Silhouette Fadeins渐显效果
- 选择TreeView控件的树状数据节点的JS方法(jquery)
- jquery 应用代码 方便的排序功能
- jquery1.4后 jqDrag 拖动 不可用
- javascript小数计算出现近似值的解决办法
- jquery 插件实现图片延迟加载效果代码
- Lazy Load 延迟加载图片的 jQuery 插件
- jquery.lazyload 实现图片延迟加载jquery插件
- 利用jQuery 实现GridView异步排序、分页的代码
- javascript contains和compareDocumentPosition 方法来确定是否HTML节点间的关系
- 使用SyntaxHighlighter实现HTML高亮显示代码的方法
- JavaScript学习笔记(十七)js 优化
- javascript 实现的完全兼容鼠标滚轴缩放图片的代码
- jQuery生成asp.net服务器控件的代码
- js实现的仿Photoshop鼠标滚轮控制输入框取值(修正兼容Chrome)
- js中鼠标滚轮事件详解(firefox多浏览器)
- JavaScript面向对象之静态与非静态类
- JavaScript 学习笔记(十六) js事件
- js左侧多级菜单动态的解决方案