-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCookies.htm
More file actions
65 lines (50 loc) · 1.88 KB
/
Cookies.htm
File metadata and controls
65 lines (50 loc) · 1.88 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<!DOCTYPE html >
<html >
<head>
<title>Cookies</title>
<script type="text/javascript">
function WriteCookie() {
if (document.myform.customer.value == "") {
alert("Enter some value!");
return;
}
cookievalue = escape(document.myform.customer.value) + ";";
document.cookie = "name=" + cookievalue;
alert("Setting Cookies : " + "name=" + cookievalue);
}
// reading cookie
function ReadCookie() {
var allcookies = document.cookie;
alert("All Cookies : " + allcookies);
// Get all the cookies pairs in an array
cookiearray = allcookies.split(';');
// Now take key value pair out of this array
for (var i = 0; i < cookiearray.length; i++) {
name = cookiearray[i].split('=')[0];
value = cookiearray[i].split('=')[1];
alert("Key is : " + name + " and Value is : " + value);
}
}
// setting expiry date
function WriteCookieExp() {
var now = new Date();
now.setMonth(now.getMonth() + 1);
//now.setMonth(now.getMonth() - 1); to Delete a cookie
cookievalue = escape(document.myform.customer.value) + ";"
document.cookie = "name=" + cookievalue;
document.cookie = "expires=" + now + ";"
alert("Setting Cookies : " + "name=" + cookievalue + "\n Expires on :"+now.toString());
}
</script>
</head>
<body>
<form name="myform" action="">
Enter name: <input type="text" name="customer"/>
<input type="button" value="Set Cookie" onclick="WriteCookie();"/>
<br />
<input type="button" value="Get Cookie" onclick="ReadCookie()"/>
<br />
<input type="button" value="Set Cookie with expiration" onclick="WriteCookieExp()"/>
</form>
</body>
</html>