【javascript基础】 JS中编码解码escape、encodeURI、encodeURIComponent区别详解【转】

JavaScript中有三对字符串编码和解码的函数:

对字符串编码的函数:escape,encodeURI,encodeURIComponent

对应字符串解码函数:unescape,decodeURI,decodeURIComponent

1 escape()函数

定义和用法

escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串。

语法

escape(string)

参数描述

string必需。要被转义或编码的字符串。

返回值

已编码的 string 的副本。其中某些字符被替换成了十六进制的转义序列。

说明

该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: * @ - _ + . / 。其他所有的字符都会被转义序列替换。

提示和注释

提示:一般使用 unescape() 对 escape() 编码的字符串进行解码(decodeURI()和decodeURIComponent也可以解码)。

注释:ECMAScript v3 反对使用该方法,应用使用 decodeURI() 和 decodeURIComponent() 替代它。

实例

在本例中,我们将使用 escape() 来编码字符串:

<script type="text/javascript">

document.write(escape("Visit W3School!") + "<br />");//编码

document.write(escape("?!=()#%&"));

var a = escape("?!=()#%&");//解码

document.write(escape(a))

</script>

输出:

Visit%20W3School%21

%3F%21%3D%28%29%23%25%26

?!=()#%&

2 encodeURI()函数

定义和用法

encodeURI() 函数可把字符串作为 URI 进行编码。

语法

encodeURI(URIstring)

参数 描述

URIstring必需。一个字符串,含有 URI 或其他要编码的文本。

返回值

URIstring 的副本,其中的某些字符将被十六进制的转义序列进行替换。

说明

该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。

该方法的目的是对 URI 进行完整的编码,因此对以下在 URI 中具有特殊含义的 ASCII 标点符号,encodeURI() 函数是不会进行转义的:;/?:@&=+$,#

提示和注释

提示:一般使用 decodeURI() 对 encodeURI() 编码的字符串进行解码(unescape()和decodeURIComponent也可以解码),如果 URI 组件中含有分隔符,

比如 ? 和 #,则应当使用 encodeURIComponent() 方法分别对各组件进行编码。

实例

在本例中,我们将使用 encodeURI() 对 URI 进行编码:

<script type="text/javascript">

document.write(encodeURI("http://www.w3school.com.cn")+ "<br />");//编码

document.write(encodeURI("http://www.w3school.com.cn/My first/"))

document.write(encodeURI(",/?:@&=+$#"))

var a = unescape("http://www.w3school.com.cn/My first/");//解码

document.write(decodeURI(a));

</script>

输出:

http://www.w3school.com.cn

http://www.w3school.com.cn/My%20first/

,/?:@&=+$#

http://www.w3school.com.cn/My first/

3 encodeURIComponent() 函数

定义和用法

encodeURIComponent() 函数可把字符串作为 URI 组件进行编码。

语法

encodeURIComponent(URIstring)

参数 描述

URIstring必需。一个字符串,含有 URI 组件或其他要编码的文本。

返回值

URIstring 的副本,其中的某些字符将被十六进制的转义序列进行替换。

说明

该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。其他字符(比如 :;/?:@&=+$,# 这些用于分隔 URI 组件的标点符号),都是由一个或多个十六进制的转义序列替换的。

提示和注释

提示:一般使用 decodeURIComponent() 对 encodeURIComponent() 编码的字符串进行解码(unescape()也可以解码(decodeURI()无法解码)),请注意 encodeURIComponent() 函数 与 encodeURI() 函数的区别之处,前者假定它的参数是 URI 的一部分(比如协议、主机名、路径或查询字符串)。因此 encodeURIComponent() 函数将转义用于分隔 URI 各个部分的标点符号。

实例

在本例中,我们将使用 encodeURIComponent() 对 URI 进行编码:

<script type="text/javascript">

document.write(encodeURIComponent("http://www.w3school.com.cn"))

document.write("<br />")

document.write(encodeURIComponent("http://www.w3school.com.cn/p 1/"))

document.write("<br />")

document.write(encodeURIComponent(",/?:@&=+$#"))

var a = encodeURIComponent("http://www.w3school.com.cn/My n first/");

document.write(decodeURIComponent(a));

</script>

输出:

http%3A%2F%2Fwww.w3school.com.cn

http%3A%2F%2Fwww.w3school.com.cn%2Fp%201%2F

%2C%2F%3F%3A%40%26%3D%2B%24%23

4 总结:

通过对三个函数的分析,我们可以知道:escape()除了 ASCII 字母、数字和特定的符号外,对传进来的字符串全部进行转义编码,因此如果想对URL编码,最好不要使用此方法。

而encodeURI() 用于编码整个URI,因为URI中的合法字符都不会被编码转换。

encodeURIComponent方法在编码单个URIComponent(指请求参数)应当是最常用的,它可以对参数中的中文、特殊字符进行转义,而不会影响整个URL。

这三个函数使用的字符集均是unicode,不同的只是escape直接对unicode编码成16进制,而encodeURI和encodeURIComponent则是把unicode编码成utf-8而已。

5 实践中使用:

(1)var r = decodeURI(document.location.search).substr(1).match(reg);//html页面传过来的参数进行解码

alert("3="+unescape(r[2]));

(2)$.get("http://127.0.0.1:8080/userVerify1/verify.do?userName=" + encodeURI(encodeURI(userName)),null,function(response){

//3.接收服务器端返回的数据,填充到div中

$("#result").html(response);

});//JQuery两次encodeURL编码,经Servlet传到服务器

var a = java.net.URLDecoder.decode(request.getParameter("userName"), "UTF-8");//服务器端进行解码