javascript怎么清除cookie


当前第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>

运行:

2.png

删除 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》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...