Learn how JavaScript COOKIES work! 🍪

preview_player
Показать описание
#JavaScript #cookies #tutorial

// cookie = a small text file stored on your computer
// used to remember information about the user
// saved in name=value pairs

Рекомендации по теме
Комментарии
Автор

const firstText =
const lastText =
const submitBtn =
const cookieBtn =

submitBtn.addEventListener("click", () => {
setCookie("firstName", firstText.value, 365);
setCookie("lastName", lastText.value, 365);
});
cookieBtn.addEventListener("click", () => {
firstText.value = getCookie("firstName");
lastText.value = getCookie("lastName");
});

function setCookie(name, value, daysToLive){
const date = new Date();
date.setTime(date.getTime() + (daysToLive * 24 * 60 * 60 * 1000));
let expires = "expires=" + date.toUTCString();
document.cookie = `${name}=${value}; ${expires}; path=/`
}
function deleteCookie(name){
setCookie(name, null, null);
}
function getCookie(name){
const cDecoded =
const cArray = cDecoded.split("; ");
let result = null;

cArray.forEach(element => {
if(element.indexOf(name) == 0){
result = element.substring(name.length + 1)
}
})
return result;
}

<!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>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<label for="firstText">first name:</label>
<input id="firstText"><br>
<label for="lastText">last name:</label>
<input id="lastText"><br>
<button
<button id="cookieBtn">get cookies</button>
<script src="index.js"></script>
</body>
</html>

BroCodez
Автор

Was supposed to use the js-cookie library, until I saw this video and decided to write it out myself. Well explained!

pexeixv
Автор

Completed the JavaScript playlist.. Thanks Bro

ALLINONE-kosv
Автор

Thank you, great practical examples, I understood cookies 😍

chiculitamihaela
Автор

this is such a life saver, thank you!!

daniellinares
Автор

Finally i can store data within a client your a lifesaver

FluoFalI
Автор

Thank you so much this tutorial was very useful and I really learned what I was doing.

mrguy
Автор

Very cool my bro code. But i'd want to suggest that you replace == with === in 08:27

TechBroLife-ukpo
Автор

Thanks. If one day my startup makes enough money I'll pay you back for this.

mahir
Автор

Thank you bro not disappointed as always, can you do a video about promises, callbacks, asyncs.. i am really struggling on those

addda
Автор

Actually, you could use the browser console and go into the application sub-menu, there, cookies, local storage and session storage are displayed in a table!

hunin
Автор

thank you so much for this valuable content bro code 🙏🙏🙏🙏🙏🙏🙏

adityashinde
Автор

Can you plz also do fortran tutorials. I need it for my exams but I can't find someone who can explain it well like you

RohitCantSing
Автор

Bro thank you for your tutorials and can you please do a course in django python?.

Ken-zhgu
Автор

Thanks for the amazing video! Where is your autocomplete? 😄

dmytro_kruhlyi
Автор

Ďakujeme za zdieľanie. Viem, že Morelogin dokáže chrániť súkromie.

fujiaaniya
Автор

sir, How to overcome document.object not defined 😔

mdmahmudjamil
Автор

i don't understand The, if(element.indexOf(name){
result = element.substring(name.length + 1);
}

is there anyone who can explain to me?

natnaeltaye
Автор

when i write document.cookie = "firstName=SpongeBob"; my cookie stays blank idk why /_ \

zibozhao
Автор

Is there any way to copy a specified cookie to your clipboard?

Kattoe