-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex12-1.html
More file actions
48 lines (47 loc) · 1.64 KB
/
ex12-1.html
File metadata and controls
48 lines (47 loc) · 1.64 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>방문 카운트 쿠키</title>
<script>
function GetCookie (name) {
let str = name+"=";
let pairs = document.cookie.split(";"); // 쿠키문자열을 ;을 경계로 분할
for(let i=0; i<pairs.length; i++) {
let pair = pairs[i].trim(); // 쿠키 앞뒤의 빈칸 제거
let unit = pair.split("=");
if(unit[0] == name)
return unescape(unit[1]);
}
return null;
}
function SetCookie (name, value, expireDate) {
let cookieStr = name + "=" + escape(value) +
((expireDate == null)?"":("; expires=" + expireDate.toUTCString()));
document.cookie = cookieStr;
}
</script></head>
<body>
<script>
let username = GetCookie("username");
console.log(username);
let count = GetCookie("count");
let expire = new Date (); // 현재 시간
if (username == null) {
count = 0;
username = prompt("이름을 입력해 주십시오.","");
if (username == null) {
alert("이름을 입력하시면 보다 나은 서비스를 제공받을 수 있습니다.");
username = "아무개";
} else {
expire.setTime(expire.getTime() + (365 * 24 * 3600 * 1000)); // 1년후
SetCookie("username",username,expire);
}
}
count++;
expire.setTime(expire.getTime() + (365 * 24 * 3600 * 1000)); // 1년후
SetCookie("count",count,expire);
document.write('<p>어서오십시오. '+username+'님의 '+count+'번째 방문을 환영합니다!');
</script>
</body>
</html>