|
@@ -0,0 +1,66 @@
|
|
|
+<!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 */
|
|
|
+ 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('address', 'harbin', 4)
|
|
|
+
|
|
|
+
|
|
|
+ function getCookie(key) {
|
|
|
+ var cookie = document.cookie
|
|
|
+ console.log(cookie)
|
|
|
+
|
|
|
+ /*
|
|
|
+ 创建一个arr数组
|
|
|
+ .split方法 把字符串拆分成数组
|
|
|
+ ["name='zs'", " password='123'", ' address=harbin']
|
|
|
+ */
|
|
|
+ var arr = cookie.split(';')
|
|
|
+ console.log(arr)
|
|
|
+ for (var i = 0; i < arr.length; i++) {
|
|
|
+ console.log(arr[i])
|
|
|
+ var tmp = arr[i].split('=')
|
|
|
+ console.log(tmp)
|
|
|
+ /* trim() */
|
|
|
+ if (tmp[0].trim() == key) {
|
|
|
+ return tmp[1]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ console.log(getCookie('name'))
|
|
|
+
|
|
|
+
|
|
|
+ function delCookie(key) {
|
|
|
+ var date = new Date()
|
|
|
+ date.setDate(date.getDate() - 1)
|
|
|
+ document.cookie = key + '=null;expires=' + date.toUTCString()
|
|
|
+ }
|
|
|
+ delCookie('password')
|
|
|
+ </script>
|
|
|
+</body>
|
|
|
+
|
|
|
+</html>
|