-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookieTest.html
69 lines (63 loc) · 1.7 KB
/
cookieTest.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
57
58
59
60
61
62
63
64
65
66
67
68
69
<!DOCTYPE html>
<html>
<body>
<script>
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + ((exdays != 0) ? ";" + expires + ";path=/" : "");
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function deleteCookie(cname) {
setCookie(cname, 0, -1);
}
function log(name, value)
{
document.getElementById("output").innerHTML += name + ((value) ? (": " + value) : "") + "<br>";
}
</script>
<div>
<button onclick="setCookie('test', 123, 1);log('setCookie done');">
SetCookie
</button>
<button onclick="log('getCookie', getCookie('test'));">
GetCookie
</button>
<button onclick="deleteCookie('test');log('deleteCookie done');">
DeleteCookie
</button>
<button onclick="log('document.cookie', document.cookie);">
GetCookieString
</button>
</div>
<div>
<button onclick="setCookie('session', 123);log('setSessionCookie done');">
SetSessionCookie
</button>
<button onclick="log('getSessionCookie', getCookie('session'));">
GetSessionCookie
</button>
<button onclick="deleteCookie('session');log('deleteSessionCookie done');">
DeleteSessionCookie
</button>
<button onclick="log('document.cookie', document.cookie);">
GetCookieString
</button>
</div>
<div id="output"></div>
</body>
</html>