查看完整版本: [-- encodeURIComponent() 函数 --]

风信Java论坛 ›› javascript/html/css 讨论交流中心 ›› encodeURIComponent() 函数 登录 -> 注册

1F encodeURIComponent() 函数   唧唧 Post by : 2008-06-11 11:40:00.0

定义和用法

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

语法

encodeURIComponent(URIstring)
参数 描述
URIstring 必需。一个字符串,含有 URI 组件或其他要编码的文本。

返回值

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

说明

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

其他字符(比如 :;/?:@&=+$,# 这些用于分隔 URI 组件的标点符号),都是由一个或多个十六进制的转义序列替换的。

提示和注释

提示:请注意 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(",/?:@&=+$#"))
</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
2F    菜鸟 Post by : 2008-06-12 23:51:49.0

不错

其实有很多人 误用成 encodeURI,这样IE或者FF都会报错的~,起码我这里就不通过编译


风信Java论坛 ›› javascript/html/css 讨论交流中心 ›› encodeURIComponent() 函数 登录 -> 注册

查看完整版本: [-- encodeURIComponent() 函数 --]
CopyRight © 2008-2009 JavaWind.Net Studio All Rights Reserved
Powered By JWind.BBS Vesion 1.0.0 Beta1 Processed in 8 ms,0 (Queries)  Gzip enabled
粤ICP备07511478号