So i'm getting started with cookies and saw this easy cookies framework provided by Mozilla,
I learned about adding a new cookie, But i'm having a weird issue when reading or getting the cookie.
okay here's buy code i'm writing:
// Cookies
const cookiesFn = () => {
$(".bg-dark-change").on('click', function () {
let bodyBlack = document.body.style.backgroundColor = "#000";
docCookies.setItem('BackgroundColor', bodyBlack, Infinity)
})
}; cookiesFn();
docCookies.getItem('BackgroundColor');
// Cookies End
as you can see above this is my code i'm trying to achieve..
ohh yea here's the frame work links: Github On Mozilla's Website
Cookies are just a dumb way of storing a piece of data in a browser that can be read later on. They don't actually do anything besides that. It's not clear whether you've actually looked to see if the cookie exists, but if your reading of the cookie is simply the same as the code you've posted, it's not enough. You actually have to do something with the cookie after reading it. For example, this code will set the background color if the cookie is present:
const backgroundColorCookie = docCookies.getItem('BackgroundColor');
if (backgroundColorCookie) {
document.body.style.backgroundColor = backgroundColorCookie;
}
$(".bg-dark-change").on('click', function () {
let bodyBlack = document.body.style.backgroundColor = "#000";
docCookies.setItem('BackgroundColor', bodyBlack, Infinity)
});