JavaScript全局函数 unescape() escape() encodeURI() decodeURI() decodeURIComponent() encodeURIComponent()


unescape() 函数可对通过 escape() 编码的字符串进行解码。

语法
unescape(string)  string必需。要解码或反转义的字符串。
返回值

string 被解码后的一个副本。

说明:
该函数的工作原理是这样的:通过找到形式为 %xx 和 %uxxxx 的字符序列(x 表示十六进制的数字),用 Unicode 字符 u00xx 和 uxxxx 替换这样的字符序列进行解码。

提示和注释
注释:ECMAScript v3 已从标准中删除了 unescape() 函数,并反对使用它,因此应该用 decodeURI() 和 decodeURIComponent() 取而代之。

<script type="text/javascript">
var test1="Visit W3School!"
test1=escape(test1)
document.write (test1")  // Visit%20W3School%21
test1=unescape(test1)
document.write(test1 ")  // Visit W3School!
</script>




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


语法
escape(string)   string必需。要被转义或编码的字符串。
返回值
已编码的 string 的副本。其中某些字符被替换成了十六进制的转义序列。

说明:
该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: * @ - _ + . / 。其他所有的字符都会被转义序列替换。
注释:ECMAScript v3 反对使用该方法,应用使用 decodeURI() 和 decodeURIComponent() 替代它。




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

语法
encodeURI(URIstring)  URIstring必需。一个字符串,含有 URI 或其他要编码的文本。
返回值
URIstring 的副本,其中的某些字符将被十六进制的转义序列进行替换。

说明
该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。
该方法的目的是对 URI 进行完整的编码,因此对以下在 URI 中具有特殊含义的 ASCII 标点符号,encodeURI() 函数是不会进行转义的:;/?:@&=+$,#

提示:如果 URI 组件中含有分隔符,比如 ? 和 #,则应当使用 encodeURIComponent() 方法分别对各组件进行编码。

<script type="text/javascript">
document.write(encodeURI("http://www.xxx.com"))            //  http://www.xxx.com
document.write(encodeURI("http://www.xxx.com/My first/"))  //  http://www.xxx.com/My%20first/
document.write(encodeURI(",/?:@&=+$#"))                    //  ,/?:@&=+$#
</script>




decodeURI() 函数
可对 encodeURI() 函数编码过的 URI 进行解码。

语法
decodeURI(URIstring)  URIstring必需。 一个字符串,含有要解码的 URI 或其他要解码的文本。
返回值 URIstring 的副本,其中的十六进制转义序列将被它们表示的字符替换。

<script type="text/javascript">
var test1="http://www.xxx.com/My first/"
document.write(encodeURI(test1))  //  http://www.xxx.com/My%20first/
document.write(decodeURI(test1))  //  http://www.xxx.com/My first/
</script>




decodeURIComponent() 函数
可对 encodeURIComponent() 函数编码的 URI 进行解码。


语法
decodeURIComponent(URIstring) URIstring必需。一个字符串,含有编码 URI 组件或其他要解码的文本。
返回值
URIstring 的副本,其中的十六进制转义序列将被它们表示的字符替换。

<script type="text/javascript">
var test1="http://www.xxx.com/My first/"  
document.write(encodeURIComponent(test1))  // http%3A%2F%2Fwww.xxx.com%2FMy%20first%2F
document.write(decodeURIComponent(test1))  // http://www.xxx.com/My first/
</script>




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

语法
encodeURIComponent(URIstring)  URIstring必需。一个字符串,含有 URI 组件或其他要编码的文本。
返回值
URIstring 的副本,其中的某些字符将被十六进制的转义序列进行替换。

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

提示:请注意 encodeURIComponent() 函数 与 encodeURI() 函数的区别之处,前者假定它的参数是 URI 的一部分(比如协议、主机名、路径或查询字符串)。
因此 encodeURIComponent() 函数将转义用于分隔 URI 各个部分的标点符号。

<script type="text/javascript">
document.write(encodeURIComponent("http://www.xxx.com"))  //   http%3A%2F%2Fwww.xxx.com
document.write(encodeURIComponent("http://www.xxx.com/My first/"))   // http%3A%2F%2Fwww.xxx.com%2FMy%20first%2F
document.write(encodeURIComponent(",/?:@&=+$#"))  //       %2C%2F%3F%3A%40%26%3D%2B%24%23
</script>

原文地址:https://www.cnblogs.com/bigdesign/p/4409196.html