jsp页面添加删除修改cookie
escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串。语法escape(string)参数描述string必需。要被转义或编码的字符串。返回值已编码的 string 的副本。其中某些字符被替换成了十六进制的转义序列。该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。其他所有的字符都会被转义序列替换。
提示和注释提示:可以使用 unescape() 对 escape() 编码的字符串进行解码。注释:ECMAScript v3 反对使用该方法,使用 decodeURI() 和 decodeURIComponent() 替代它。
在本例中,我们将使用 escape() 来编码字符串:
<script type="text/javascript">
document.write(escape("Visit W3School!") + "
")
document.write(escape("?!=()#%&"))
</script>
输出:
Visit%20W3School%21
%3F%21%3D%28%29%23%25%26画面加载时,添加cookie
<%
Cookie cookieName = new Cookie("name", "abc");
Cookie cookieAge = new Cookie("age", "17");
response.addCookie(cookieName );
response.addCookie(cookieAge );
%>
function deleteCookie(name, path, domain) {
alert(name);
var exp = new Date();
exp.setTime(exp.getTime() - 1);
//指定cookie的name,仅仅消除指定的cookie
var cookie = escape(name) + "=xxx;" + "expires=" + exp.toUTCString()
+ ((path == null) ? "" : ";path=" + path)
+ ((domain == null) ? "" : ";domain=" + domain);
alert(cookie);
document.cookie = cookie;
}function modifyCookie() {
document.cookie = 'name=' +document.getElementById("name").value;
document.cookie = 'age='
+ document.getElementById("age").value;
}