-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetgetcookie.html
45 lines (34 loc) · 1.16 KB
/
setgetcookie.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
<html>
<head>
<title> Cookie Creation</title>
</head>
<body onload="getCookie()">
<label> name: </label>
<input type="text" id="name" /><br><br>
<label> age: </label>
<input type="number" id="age" /><br><br>
<input type="button" value="Set Cookie" onclick="setCookie()" />
<p id="content"></p>
<script>
function setCookie() {
var nameValue = document.getElementById("name").value;
var ageValue = document.getElementById("age").value;
document.cookie = "name=" + nameValue
document.cookie = "age=" + ageValue;
getCookie();
}
function getCookie() {
var cookiearr = document.cookie.split(";");
for (i = 0; i < cookiearr.length; i++) {
var pair = cookiearr[i].trim().split("=");
if (pair[0] === "name") {
var name = pair[1];
} else if (pair[0] === "age") {
var age = pair[1];
}
}
document.getElementById("content").innerHTML = "Name: " + name + ", Age: " + age;
}
</script>
</body>
</html>