123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <script>
- /*
- cookie 本地存储 大小 4K 默认的有效期 当前对话窗口
- 可以通过expires 设置过期时间
- */
- document.cookie = "name='zs'"
- var date = new Date()
- date.setDate(date.getDate() + 1)
- console.log(date)
- console.log(date.toUTCString())
- document.cookie = "password= '123';expires=" + date.toUTCString()
- function setCookie (key,value,expires){
- var date = new Date()
- date.setDate(date.getDate() + expires)
- document.cookie = key + '=' + value + ';expires=' + date.toUTCString()
- }
- setCookie()
- function getCookie (key) {
- var cookie = document.cookie
- console.log(cookie)
- /*
- 创建一个数组 存放分开这个字符串
- .split 把字符串 拆分成数组
- ["password='123'", ' undefined=undefined', " name='zs'"]
- */
- var arr = cookie.split(';')
- console.log(arr)
- for(var i=0;i<arr.length;i++){
- var tmp = arr[i].split('=')
- console.log(tmp)
- /* trim() 方法 用于删除字符串的头尾空白符 */
- if(tmp[0].trim() == key){
- return tmp[1]
- }
- }
- }
- console.log(getCookie('undefined'))
- function delCookie(key){
- var date = new Date()
- date.setDate(date.getDate() - 1 )
- document.cookie = key + '=null;expires=' + date.toUTCString()
- }
- delCookie('password')
- </script>
- </body>
- </html>
|