使用Ajax时常用的转码方法encodeURI,escape,encodeURI
2012-12-19 16:47
381 查看
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=中国asp之家&ct=21);
用法:
页面javascript:
...
var url =
'policy.do?method=searchSuggest&word=' +
str+"&now="+new
Date().getTime();
url =
encodeURI(url);
url =
encodeURI(url);
...
后台程序中:
...
String word
=
request.getParameter("word");
word =
java.net.URLDecoder.decode(word,"UTF-8");
...
3、
js使用数据时可以使用escape
例如:搜藏中history纪录。
4、
escape对0-255以外的unicode值进行编码时输出%u****格式,其它情况下escape,encodeURI,encodeURIComponent编码结果相同。
最多使用的应为encodeURIComponent,它是将中文、韩文等特殊字符转换成utf-8格式的url编码,所以如果给后台传递参数需要使用
encodeURIComponent时需要后台解码对utf-8支持(form中的编码方式和当前页面编码方式相同)(中国asp之家注:就是说如果你
的页面编码是gb2312的话,服务器端接收的将是乱码)
escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z
encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z
encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z
附上这个三个函数的介绍:
escape 方法
对 String
对象编码以便它们能在所有计算机上可读,
escape(charString)
必选项
charstring 参数是要编码的任意 String 对象或文字。
说明
escape 方法返回一个包含了 charstring 内容的字符串值( Unicode 格式)。所有空格、标点、重音符号以及其他非
ASCII 字符都用 %xx 编码代替,其中 xx 等于表示该字符的十六进制数。例如,空格返回的是 "%20" 。
字符值大于 255
的以 %uxxxx 格式存储。
注意 escape
方法不能够用来对统一资源标示码 (URI) 进行编码。对其编码应使用 encodeURI 和encodeURIComponent
方法。
encodeURI 方法
将文本字符串编码为一个有效的统一资源标识符 (URI)。
encodeURI(URIString)
必选的
URIString 参数代表一个已编码的 URI。
说明
encodeURI 方法返回一个编码的 URI。如果您将编码结果传递给 decodeURI,那么将返回初始的字符串。encodeURI
方法不会对下列字符进行编码:":"、"/"、";" 和 "?"。请使用 encodeURIComponent
方法对这些字符进行编码。
encodeURIComponent 方法
将文本字符串编码为一个统一资源标识符 (URI) 的一个有效组件。
encodeURIComponent(encodedURIString)
必选的
encodedURIString 参数代表一个已编码的 URI 组件。
说明
encodeURIComponent 方法返回一个已编码的 URI。如果您将编码结果传递给
decodeURIComponent,那么将返回初始的字符串。因为 encodeURIComponent
方法对所有的字符编码,请注意,如果该字符串代表一个路径,例如
/folder1/folder2/default.html,其中的斜杠也将被编码。这样一来,当该编码结果被作为请求发送到 web
服务器时将是无效的。如果字符串中包含不止一个 URI 组件,请使用 encodeURI 方法进行编码。
原文连接:http://blog.sina.com.cn/s/blog_61dfec800100iql8.html
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=中国asp之家&ct=21);
用法:
页面javascript:
...
var url =
'policy.do?method=searchSuggest&word=' +
str+"&now="+new
Date().getTime();
url =
encodeURI(url);
url =
encodeURI(url);
...
后台程序中:
...
String word
=
request.getParameter("word");
word =
java.net.URLDecoder.decode(word,"UTF-8");
...
3、
js使用数据时可以使用escape
例如:搜藏中history纪录。
4、
escape对0-255以外的unicode值进行编码时输出%u****格式,其它情况下escape,encodeURI,encodeURIComponent编码结果相同。
最多使用的应为encodeURIComponent,它是将中文、韩文等特殊字符转换成utf-8格式的url编码,所以如果给后台传递参数需要使用
encodeURIComponent时需要后台解码对utf-8支持(form中的编码方式和当前页面编码方式相同)(中国asp之家注:就是说如果你
的页面编码是gb2312的话,服务器端接收的将是乱码)
escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z
encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z
encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z
附上这个三个函数的介绍:
escape 方法
对 String
对象编码以便它们能在所有计算机上可读,
escape(charString)
必选项
charstring 参数是要编码的任意 String 对象或文字。
说明
escape 方法返回一个包含了 charstring 内容的字符串值( Unicode 格式)。所有空格、标点、重音符号以及其他非
ASCII 字符都用 %xx 编码代替,其中 xx 等于表示该字符的十六进制数。例如,空格返回的是 "%20" 。
字符值大于 255
的以 %uxxxx 格式存储。
注意 escape
方法不能够用来对统一资源标示码 (URI) 进行编码。对其编码应使用 encodeURI 和encodeURIComponent
方法。
encodeURI 方法
将文本字符串编码为一个有效的统一资源标识符 (URI)。
encodeURI(URIString)
必选的
URIString 参数代表一个已编码的 URI。
说明
encodeURI 方法返回一个编码的 URI。如果您将编码结果传递给 decodeURI,那么将返回初始的字符串。encodeURI
方法不会对下列字符进行编码:":"、"/"、";" 和 "?"。请使用 encodeURIComponent
方法对这些字符进行编码。
encodeURIComponent 方法
将文本字符串编码为一个统一资源标识符 (URI) 的一个有效组件。
encodeURIComponent(encodedURIString)
必选的
encodedURIString 参数代表一个已编码的 URI 组件。
说明
encodeURIComponent 方法返回一个已编码的 URI。如果您将编码结果传递给
decodeURIComponent,那么将返回初始的字符串。因为 encodeURIComponent
方法对所有的字符编码,请注意,如果该字符串代表一个路径,例如
/folder1/folder2/default.html,其中的斜杠也将被编码。这样一来,当该编码结果被作为请求发送到 web
服务器时将是无效的。如果字符串中包含不止一个 URI 组件,请使用 encodeURI 方法进行编码。
原文连接:http://blog.sina.com.cn/s/blog_61dfec800100iql8.html
相关文章推荐
- 使用Ajax时常用的转码方法encodeURI,escape,encodeURIComponent
- escape,encodeURI,encodeURIComponent方法使用
- escape,encodeURI,encodeURIComponent的使用方法
- JSP、AJax中文乱码问题解决,escape(), encodeURI(), encodeURIComponent(),js对参数连续两次调用 encodeURI(String) 方法
- JSP、AJax中文乱码问题解决,escape(), encodeURI(), encodeURIComponent(),js对参数连续两次调用 encodeURI(String) 方法
- escape(), encodeURI()和encodeURIComponent()是在Javascript中用于编码字符串的三个常用的方法.
- escape, unescape, encodeURI, decodeURI, encodeURIComponent, decodeURIComponent 等方法的对比分析
- 使用escape、encodeURI 和 encodeURIComponent 解决url中文乱码问题
- URL编码方法比较 Encode,Javascript,escape,encodeURI,encodeURIComponent,UTF-8
- JavaScript中URL编码方法比较 escape,encodeURI,encodeURIComponent
- escape, encodeURI, encodeURIComponent 的使用情况
- 使用escape、encodeURI 和 encodeURIComponent 解决url中文乱码问题
- escape、encodeURI、encodeURIComponent等方法的区别
- 结合实例详细介绍encodeURI()、encodeURIComponent()、decodeURI()、decodeURIComponent()使用方法
- js文字编码函数escape,encodeURI,encodeURIComponent 的使用
- URL 编码方法比较 Encode,Javascript,escape,encodeURI,encodeURIComponent,UTF-8
- Js中escape(),encodeURI()和encodeURIComponent()使用和比较[转载]
- 使用escape、encodeURI 和 encodeURIComponent 解决url中文乱码问题
- escape、encodeURI、encodeURIComponent等方法的区别比较
- 使用encodeURIComponent方法对url中的特殊字符进行转码