当前第2页 返回上一页
事例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <html>
<head>
<script type = "text/javascript" >
<!--
function WriteCookie() {
var now = new Date();
now.setMonth( now.getMonth() + 1 );
cookievalue = escape(document.myform.customer.value) + ";"
document.cookie = "name=" + cookievalue;
document.cookie = "expires=" + now.toUTCString() + ";"
document.write ( "Setting Cookies : " + "name=" + cookievalue );
}
</script>
</head>
<body>
<form name = "myform" action = "" >
Enter name: <input type = "text" name = "customer" />
<input type = "button" value = "Set Cookie" onclick = "WriteCookie()" />
</form>
</body>
</html>
|
运行:

删除 cookie
删除 cookie 非常简单,不必指定 cookie 值:直接把 expires 参数设置为过去的日期即可:
document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
应该定义 cookie 路径以确保删除正确的 cookie。如果不指定路径,有些浏览器不会让咱们删除 cookie。
事例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <html>
<head>
<script type = "text/javascript" >
<!--
function WriteCookie() {
var now = new Date();
now.setMonth( now.getMonth() - 1 );
cookievalue = escape(document.myform.customer.value) + ";"
document.cookie = "name=" + cookievalue;
document.cookie = "expires=" + now.toUTCString() + ";"
document.write( "Setting Cookies : " + "name=" + cookievalue );
}
</script>
</head>
<body>
<form name = "myform" action = "" >
Enter name: <input type = "text" name = "customer" />
<input type = "button" value = "Set Cookie" onclick = "WriteCookie()" />
</form>
</body>
</html>
|
更多编程相关知识,请访问:编程视频!!
以上就是javascript怎么清除cookie的详细内容,更多文章请关注木庄网络博客!
返回前面的内容
相关阅读 >>
nodejs的爬虫框架superagent
聊聊javascript中eval()函数的用法
javascript怎么删除dom
javascript $是什么意思
object.fromentries和object.entries的使用
javascript创建命名空间的多种玩法
javascript怎么去除空格
分享es2019中值得收藏的8个有用功能
javascript中什么是字符串
javascript怎么删除类
更多相关阅读请进入《javascript》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » javascript怎么清除cookie