Skip to content

Commit 7202949

Browse files
cookie
1 parent aecf692 commit 7202949

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed
Loading
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<title>cookies-JS</title>
9+
<link rel="shortcut icon" href="images/js-logo.png" type="image/x-icon">
10+
</head>
11+
12+
<body>
13+
<h1>Cookies in JS</h1>
14+
<h2>Example One</h2>
15+
<p id="textOne"></p>
16+
<h2>setCookie</h2>
17+
<p id="textTwo"></p>
18+
<h2>getCookie</h2>
19+
<p id="textThree"></p>
20+
<h2>checkCookie</h2>
21+
<p id="textFour"></p>
22+
<script src="script.js"></script>
23+
</body>
24+
25+
</html>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
You should have heard about cookie while using a website, which always pops up at you, and some people say it is harmfull because it collects data from us.
3+
Let's get indepth of cookie...
4+
>> Cookies are data, stored in small text files on your computer
5+
> Cookies were invented to solve the problem of "how to remember information about the user"
6+
> when a user visits a web page his name can be stored in a cookie
7+
> next time the user visits the web page he can remember his name
8+
We will try by our first Example
9+
10+
Example 1:
11+
*/
12+
let textOne = document.getElementById('textOne');
13+
14+
let rememberPersonOne = document.cookie = "username= John Doe";
15+
16+
textOne.innerHTML = rememberPersonOne;
17+
18+
// Cookies can be changed or deleted the same way that they have been created
19+
// for Example
20+
document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
21+
22+
23+
/* Now there are three main functions of cookies
24+
1. setCookie()
25+
2. getCookie()
26+
3. checkCookie()
27+
*/
28+
29+
// In this Example we will discuss about how to setCookie()
30+
31+
let textTwo = document.getElementById('textTwo');
32+
33+
function setCookie(cname, cvalue, exdays) {
34+
const d = new Date();
35+
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
36+
let expires = "expires=" + d.toUTCString();
37+
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
38+
}
39+
textTwo.innerHTML = setCookie.apply();
40+
41+
42+
// getCookie
43+
44+
function getCookie(cname) {
45+
let name = cname + "=";
46+
let decodedCookie = decodeURIComponent(document.cookie);
47+
let ca = decodedCookie.split(';');
48+
for (let i = 0; i < ca.length; i++) {
49+
let c = ca[i];
50+
while (c.charAt(0) == ' ') {
51+
c = c.substring(1);
52+
}
53+
if (c.indexOf(name) == 0) {
54+
return c.substring(name.length, c.length);
55+
}
56+
}
57+
return "";
58+
}
59+
textThree.innerHTML = getCookie();
60+
61+
// checkCookie
62+
63+
let textFour = document.getElementById('textFour');
64+
65+
function checkCookie() {
66+
let username = getCookie("username");
67+
if (username != "") {
68+
alert("Welcome again " + username);
69+
} else {
70+
username = prompt("Please enter your name:", "");
71+
if (username != "" && username != null) {
72+
setCookie("username", username, 365);
73+
}
74+
}
75+
}
76+
textFour.innerHTML = checkCookie();

0 commit comments

Comments
 (0)