Skip to content

cookie #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions JavaScript/Advance/BOM/8.cookie/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>cookies-JS</title>
<link rel="shortcut icon" href="images/js-logo.png" type="image/x-icon">
</head>

<body>
<h1>Cookies in JS</h1>
<h2>Example One</h2>
<p id="textOne"></p>
<h2>setCookie</h2>
<p id="textTwo"></p>
<h2>getCookie</h2>
<p id="textThree"></p>
<h2>checkCookie</h2>
<p id="textFour"></p>
<script src="script.js"></script>
</body>

</html>
76 changes: 76 additions & 0 deletions JavaScript/Advance/BOM/8.cookie/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
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.
Let's get indepth of cookie...
>> Cookies are data, stored in small text files on your computer
> Cookies were invented to solve the problem of "how to remember information about the user"
> when a user visits a web page his name can be stored in a cookie
> next time the user visits the web page he can remember his name
We will try by our first Example

Example 1:
*/
let textOne = document.getElementById('textOne');

let rememberPersonOne = document.cookie = "username= John Doe";

textOne.innerHTML = rememberPersonOne;

// Cookies can be changed or deleted the same way that they have been created
// for Example
document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";


/* Now there are three main functions of cookies
1. setCookie()
2. getCookie()
3. checkCookie()
*/

// In this Example we will discuss about how to setCookie()

let textTwo = document.getElementById('textTwo');

function setCookie(cname, cvalue, exdays) {
const d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
let expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
textTwo.innerHTML = setCookie.apply();


// getCookie

function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
textThree.innerHTML = getCookie();

// checkCookie

let textFour = document.getElementById('textFour');

function checkCookie() {
let username = getCookie("username");
if (username != "") {
alert("Welcome again " + username);
} else {
username = prompt("Please enter your name:", "");
if (username != "" && username != null) {
setCookie("username", username, 365);
}
}
}
textFour.innerHTML = checkCookie();