这些URI方法encodeURI、encodeURIComponent()、decodeURI()、decodeURIComponent()代替了BOM的escape()和unescape()方法。URI方法更可取,因为它们对所有Unicode符号编码,而BOM方法只能对ASCII符号正确编码。尽量避免使用escape()和unescape()方法。摘自 javascript advanced book.

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=百度&ct=21);

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

例如:搜藏中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 的一部分

javascript中escape()、unescape()、encodeURI()、encodeURIComponent()、decodeURI()、decodeURIComponent()比较的更多相关文章

  1. Javascript中escape()、encodeURI()、encodeURIComponent()的区别

    JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decod ...

  2. URI 方法 encodeURI() encodeURIComponent() docodeURI() decodeURIComponent()

    URI 方法  encodeURI()  encodeURIComponent()  docodeURI()  decodeURIComponent()   var sUri = “http://ww ...

  3. Javascript中escape(), encodeURI()和encodeURIComponent()之精析与比较

    escape(), encodeURI()和encodeURIComponent()是在Javascript中用于编码字符串的三个常用的方法,而他们之间的异同却困扰了很多的Javascript初学者, ...

  4. 【javascript】escape()、encodeURI()、encodeURIComponent()区别详解

    JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decod ...

  5. JS中escape()、encodeURI()、encodeURIComponent()区别详解

    avaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decode ...

  6. javascript之url转义escape()、encodeURI()和decodeURI()

    我们可以知道:escape()除了 ASCII 字母.数字和特定的符号外,对传进来的字符串全部进行转义编码,因此如果想对URL编码,最好不要使用此方法.而encodeURI() 用于编码整个URI,因 ...

  7. javascript之url转义escape()、encodeURI()和decodeURI(),ifram父子传参参数有中文时出现乱码

    ifram父子传参参数有中文时出现乱码,可先在父级页面用encodeURI转义,在到子页面用进行decodeURI()解码 我们可以知道:escape()除了 ASCII 字母.数字和特定的符号外,对 ...

  8. js 中escape,encodeURI,encodeURIComponent的区别

    escape:方法不能能够用来对统一资源(URI)进行编码,对其编码应使用encodeURI和encodeURIComponent encodeURI:encodeURI ()方法返回一个编码的 UR ...

  9. escape,unescape与encodeURIComponent,decodeURIComponent

    escape:将string转成unicode字符串 escape('

随机推荐

  1. 正则匹配中文 UTF-8 & GBK

    在php 中: //GB2312汉字字母数字下划线正则表达式 GBK: preg_match("/^[".chr(0xa1)."-".chr(0xff).&qu ...

  2. aspnet Global文件概况

    <%@ Application Language="C#" %> <script runat="server">        void ...

  3. Lombok简化Java代码

    导包:import lombok.Data; Lombok简化Java代码: 在Lombok中,生成构造方法的annotation一共有三个:@NoArgsConstructor, @Required ...

  4. linux环境下安装tomcat并配置tomcat日志分割

    1.直接解压apache-tomcat-7.0.69.tar.gz 存放在/home目录下 根据需要自定义tomcat名称 mv apache-tomcat-7.0.69 Tomcat7 2.解压cr ...

  5. c# 字符串操作

    一.字符串操作 //字符串转数组 string mystring="this is a string" char[] mychars=mystring.ToCharArray(); ...

  6. Pegasos: Primal Estimated sub-GrAdient Solver for SVM

    Abstract We describe and analyze a simple and effective iterative algorithm for solving the optimiza ...

  7. python获取指定时间段内的随机不重复的时间点

    上篇 <python时间时分秒与秒数的互相转换>http://www.cnblogs.com/gayhub/p/6154707.html 提到了把时间转成秒数的方法, 这篇写写转换成秒数后 ...

  8. mysql配置文件编写

    实例: 首先建立 mysql的配置文件 dbconfig.php <?php $phpexcel=array( "host"=>'localhost',//127.0. ...

  9. mysql 查看数据库、表的基本命令

    1:show databases; 查看所有的数据库,等同于select schema_name from information_schema.schemata\G.\G 替换;,以纵向报表的形式输 ...

  10. iOS开发笔记1:[转]导航栏里的"Back"按钮显示不出来

    最近项目中遇到一个问题,push过去的ViewController的Nav上面没有返回按钮,遂搜索资料,找到了以下文档.经测试i,问题解决. 原文地址:http://www.cnblogs.com/s ...