|
@@ -0,0 +1,68 @@
|
|
|
+<!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 = 'xiaoming'"
|
|
|
+
|
|
|
+ var date = new Date()
|
|
|
+ date.setDate(date.getDate() + 1)
|
|
|
+ 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.split(';'))
|
|
|
+
|
|
|
+ /*
|
|
|
+ .split 方法 去拆分字符串
|
|
|
+ */
|
|
|
+ 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]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ getCookie()
|
|
|
+
|
|
|
+ console.log(getCookie('address'))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ function delCookie(key) {
|
|
|
+ var date = new Date()
|
|
|
+ date.setDate(date.getDate() - 1)
|
|
|
+ document.cookie = key + '=null;expires=' + date.toUTCString()
|
|
|
+ }
|
|
|
+ delCookie('password')
|
|
|
+ </script>
|
|
|
+</body>
|
|
|
+
|
|
|
+</html>
|