7_cookie.html 995 B

12345678910111213141516171819202122232425262728
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. //创建cookie
  11. let timer = new Date();
  12. timer.setDate(timer.getDate()+3);
  13. document.cookie = "username=zhangsan;expires="+timer.toUTCString();
  14. document.cookie = "age=18;expires="+timer.toUTCString();
  15. document.cookie = "sex=man;expires="+timer.toUTCString();
  16. //获取cookie
  17. console.log(document.cookie);
  18. // 删除cookie
  19. timer.setDate(timer.getDate()-10);
  20. document.cookie = "username=;expires="+timer.toUTCString();
  21. // 实现以下方法
  22. setCookie("username","zhangsan",3);//设置cookie 三个参数 第一个是cookie名 第二个是cookie值 第三个是过期时间
  23. getCookie("username");//获取cookie
  24. removeCookie("username");//删除cookie
  25. </script>
  26. </body>
  27. </html>