escape:可对字符串进行编码,不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。其他所有的字符都会被转义序列替换。
encodeURI:可把字符串作为URI进行编码,encodeURI方法不会对下列字符进行编码:":"、"/"、";" 和 "?"等特殊字符
js:
var name1=document.getElementById("key").value;
var name=encodeURI(name);
window.location.href="a.jsp?name="+name;
java:
String name1 = request.getParameter("name");
String name = name1.getBytes("ISO-8859-1","UTF-8");
encodeURIComponent:可把字符串作为URI组件进行编码
js:
var name1 = document.getElementById("key").value;
var name = encodeURIComponent(encodeURIComponent(name1));
java:
String name1 = request.getParameter("name");
String name = URLDecorder.decode(name1,"UTF-8");
例子:(转载)
1 escape()
<script type="text/javascript"> document.write(escape("http://www.w3school.com.cn") + "<br />") document.write(escape("?!=()#%&")) </script>
输出:
http%3A//www.w3school.com.cn %3F%21%3D%28%29%23%25%26
2 encodeURI()
<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(",/?:@&=+$#")) </script>
输出:
http://www.w3school.com.cn http://www.w3school.com.cn/My%20first/ ,/?:@&=+$# 对整个URL进行编码,而URL的特定标识符不会被转码。 3 encodeURIComponent() <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>
输出: