带参数URL测试---使用encodeURIComponent 函数对传参编码

2010-05-10  金鑫 

 [如需转载,请在转载时注明出处,并保证本文的完整性]

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

   注意:encodeURIComponent不编码字符有71个:! ''()*-._~0-9a-zA-Z

     encodeURIComponent 方法将文本字符串编码为一个统一资源标识符 (URI) 的一个有效组件。

 

说明:
      encodeURIComponent
方法返回一个已编码的 URI。当然你也可以将编码结果传递给 decodeURIComponent,那么将返回初始的字符串。因为 encodeURIComponent 方法对所有的字符编码,请注意,如果该字符串代表一个路径,例如 /folder1/folder2/test.html,其中的斜杠也将被编码。这样一来,当该编码结果被作为请求发送到 web 服务器时将是无效的。


注意:如果字符串中包含不止一个 URI 组件,请使用 encodeURI 方法进行。 

 

示例:

1、编写一个简单的test.html页面用于转换编码

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>转换参数编码测试</title>

</head>

<body>

<script type="text/javascript">

var source = "文字参数";

document.write(encodeURIComponent(source));

</script>

</body>

</html>

2、打开html页面,打印显示已编码的参数

3、拼装带参数的URL,进行测试

589°/5881 人阅读/1 条评论 发表评论

熊志男  2010-09-07

应用在哪里呢?


登录 后发表评论