-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
56 lines (56 loc) · 1.6 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>创建一个简答cookie</title>
<style>
div{
margin-top: 100px;
text-align: center;
font-size: 36px;
}
@media screen and (max-width:480px){
div{
margin-top: 20px;
text-align: center;
font-size: 22px;
}
}
</style>
<script>
//设置cookie(创建)
function setCookie(cname,cvalue,exdays){ //cookie 名===>cname、cookie 值===>cvalue、cookie过期时间===>exdays
var d = new Date(); //获取date对象
d.setTime(d.getTime()+(exdays*24*60*60*1000)); //设置当前时间毫秒
var expires = "expires=" +d.toUTCString(); //把时间转换为字符串
document.cookie = cname+"="+cvalue+"; "+expires; //输出结果
}
//获取cookie(获取)
function getCookie(cname){
var name = cname + "=";
var ca = document.cookie.split(';'); //基于;进行分割
for(var i=0; i<ca.length; i++) { //对ca数组进行循环取出
var c = ca[i].trim();
if (c.indexOf(name)==0) { return c.substring(name.length,c.length); }
}
return "";
}
//检查cookie
function checkCookie(){
var user=getCookie("username"); //获取cookie对象
if (user!="" && user!=null ){ //判断cookie是否保存了用户信息
alert("欢迎 " + user + " 再次访问"); //警告框
}
else {
user = prompt("请输入你的名字:",""); //提示框
if (user!="" && user!=null){
setCookie("username",user,30);
}
}
}
</script>
</head>
<body>
<div>这是一个简单的cookie</div>
</body>
</html>