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!
This condition checks if the current cookie string (element) starts with the specified cookie name. indexOf(name) returns the index of the first occurrence of name in the element. If the cookie name is at the start of the string, it will return 0. This means that you are checking for a cookie that matches exactly (e.g., if name is "firstName", it will check for "firstName=value").
This code is giving errors i copied it from the comment section to see if i made any mistakes but it still gave the same error function getCookie(name){ const cDecoded = decodeURIComponent(document.cookie); const cArray = cDecoded.split("; "); let result = null;
cArray.forEach(element => { if(element.indexOf(name) == 0){ result = element.substring(name.length + 1) } }) return result; } error TypeError: Cannot read properties of undefined (reading 'length') at javaScript.js:25:39 at Array.forEach () at getCookie (javaScript.js:23:9) at javaScript.js:3:1 (anonymous) @ javaScript.js:25 getCookie @ javaScript.js:23 (anonymous) @ javaScript.js:3
const firstText = document.querySelector("#firstText");
const lastText = document.querySelector("#lastText");
const submitBtn = document.querySelector("#submitBtn");
const cookieBtn = document.querySelector("#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 = decodeURIComponent(document.cookie);
const cArray = cDecoded.split("; ");
let result = null;
cArray.forEach(element => {
if(element.indexOf(name) == 0){
result = element.substring(name.length + 1)
}
})
return result;
}
Document
first name:
last name:
submit
get cookies
Was supposed to use the js-cookie library, until I saw this video and decided to write it out myself. Well explained!
Completed the JavaScript playlist.. Thanks Bro
Thank you, great practical examples, I understood cookies 😍
Finally i can store data within a client your a lifesaver
this is such a life saver, thank you!!
Thanks. If one day my startup makes enough money I'll pay you back for this.
🤣
We need more people like you 👏
Thank you so much this tutorial was very useful and I really learned what I was doing.
super underrated!!!
Thank you bro not disappointed as always, can you do a video about promises,callbacks,asyncs.. i am really struggling on those
He already did
Great tutorial. Thank you
awesome explanation!
Awesome bro 😎👍
That was complicated 🥵🥵🥵
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!
Thanks!
Bro thank you for your tutorials and can you please do a course in django python?.
thank you so much for this valuable content bro code 🙏🙏🙏🙏🙏🙏🙏
great video
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
wow greate tutorial
Thanks for the amazing video! Where is your autocomplete? 😄
i don't understand The , if(element.indexOf(name){
result = element.substring(name.length + 1);
}
is there anyone who can explain to me?
This condition checks if the current cookie string (element) starts with the specified cookie name.
indexOf(name) returns the index of the first occurrence of name in the element. If the cookie name is at the start of the string, it will return 0.
This means that you are checking for a cookie that matches exactly (e.g., if name is "firstName", it will check for "firstName=value").
Variables can work too?
Very cool my bro code. But i'd want to suggest that you replace == with === in 08:27
thank bro
sir, How to overcome document.object not defined 😔
is this what consistency is?
Yup
Ďakujeme za zdieľanie. Viem, že Morelogin dokáže chrániť súkromie.
didnt understand the getCookie function can anyone pls help?
when i write document.cookie = "firstName=SpongeBob"; my cookie stays blank idk why /_ \
Is there any way to copy a specified cookie to your clipboard?
the video title says it all hahaha
W bro
lets go
Remove the word "Beginners" from the playList title
cookies dont work, they stay blank xd
first comment
This code is giving errors i copied it from the comment section to see if i made any mistakes but it still gave the same error
function getCookie(name){
const cDecoded = decodeURIComponent(document.cookie);
const cArray = cDecoded.split("; ");
let result = null;
cArray.forEach(element => {
if(element.indexOf(name) == 0){
result = element.substring(name.length + 1)
}
})
return result;
}
error
TypeError: Cannot read properties of undefined (reading 'length')
at javaScript.js:25:39
at Array.forEach ()
at getCookie (javaScript.js:23:9)
at javaScript.js:3:1
(anonymous) @ javaScript.js:25
getCookie @ javaScript.js:23
(anonymous) @ javaScript.js:3