escape,encodeURI,encodeURIComponent函数比较

作者:chendehui4work 来源:chendehui4work的blog 时间:2008-01-27 11:19:00 

js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent

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=asp之家&ct=21);

3、  js使用数据时可以使用escape

例如:搜藏中history纪录。

4、  escape对0-255以外的unicode值进行编码时输出%u****格式,其它情况下escape,encodeURI,encodeURIComponent编码结果相同。

最多使用的应为encodeURIComponent,它是将中文、韩文等特殊字符转换成utf-8格式的url编码,所以如果给后台传递参数需要使用encodeURIComponent时需要后台解码对utf-8支持(form中的编码方式和当前页面编码方式相同)

(asp之家注:就是说如果你的页面编码是gb2312的话,服务器端接收的将是乱码)

escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z

encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z

encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z

标签:escape,encodeURI,encodeURIComponent,编码,url
0
投稿

猜你喜欢

  • 如何创建 Firefox 的 Jetpack 扩展

    2009-10-13 20:55:00
  • ASP UTF-8编码下字符串截取和获取长度函数

    2011-03-30 10:52:00
  • 自定义用于ASP Web站点的 SQL 7.0 数据库

    2008-10-28 21:09:00
  • Mysql中explain的使用详解

    2009-12-08 16:18:00
  • 100%点击区的滑动门

    2007-06-14 22:00:00
  • Web2.0时代的网页视觉设计

    2008-07-13 14:07:00
  • 一个数组随机不重复问题

    2008-09-02 20:05:00
  • 添加到各大流行网摘 书签的代码

    2008-04-20 14:15:00
  • 如何使用数组来显示下拉菜单?

    2010-05-16 15:19:00
  • 设置密码保护的SqlServer数据库备份文件与恢复文件的方法

    2011-11-03 16:55:30
  • 什么是gzip,人肉gzip?

    2008-11-20 13:01:00
  • 解决方案,而不是功能

    2011-01-30 18:11:00
  • 用ASP编程实现网络内容快速查找

    2007-09-16 17:56:00
  • 从MySQL4.0向MySQL5迁移数据

    2007-11-19 13:11:00
  • TXT.WORD文档下载另存为,而不是在浏览器中打开

    2007-10-25 11:43:00
  • MS Server和Oracle中对NULL处理的一些细节差异

    2009-06-10 17:35:00
  • AJAX概述

    2007-11-03 13:41:00
  • 用XMLHTTPRequest对象进行客户端验证

    2008-03-03 12:38:00
  • 服务器安装MySQL教程及注意事项

    2008-11-11 12:12:00
  • [译]艺术和设计的差异 (1)

    2009-09-25 12:38:00
  • asp之家 网络编程 m.aspxhome.com