12345678910111213141516171819202122232425262728 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <script>
- //创建cookie
- let timer = new Date();
- timer.setDate(timer.getDate()+3);
- document.cookie = "username=zhangsan;expires="+timer.toUTCString();
- document.cookie = "age=18;expires="+timer.toUTCString();
- document.cookie = "sex=man;expires="+timer.toUTCString();
- //获取cookie
- console.log(document.cookie);
- // 删除cookie
- timer.setDate(timer.getDate()-10);
- document.cookie = "username=;expires="+timer.toUTCString();
-
- // 实现以下方法
- setCookie("username","zhangsan",3);//设置cookie 三个参数 第一个是cookie名 第二个是cookie值 第三个是过期时间
- getCookie("username");//获取cookie
- removeCookie("username");//删除cookie
- </script>
- </body>
- </html>
|