zsydgithub 2 years ago
parent
commit
840885d297
2 changed files with 89 additions and 0 deletions
  1. 60 0
      8_html5/10_本地存储.html
  2. 29 0
      8_html5/11_本地存储2.html

+ 60 - 0
8_html5/10_本地存储.html

@@ -0,0 +1,60 @@
+<!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>

+ 29 - 0
8_html5/11_本地存储2.html

@@ -0,0 +1,29 @@
+<!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>
+    /* 
+    localStorage.key = value
+    设置本地存储 大小5M
+    一直存储 除非被删除
+    */
+    // localStorage.isLogin = true
+    // console.log(localStorage.isLogin)
+
+    // localStorage.setItem('name','lisi')
+
+    /* 
+    sessionStorage 
+    大小5M  
+    会话窗口关闭失效
+    */
+    // sessionStorage.age = 30
+  </script>
+</body>
+</html>