We need to implement browser cookie functionality to persist user preferences across browser sessions. The key requirements are:
Input:
Output:
// Cookie utility function to get a specific cookie value
function getCookie(name) {
// Convert cookie string to array of individual cookies
const cookies = document.cookie.split('; ');
// Find the specific cookie we want
const cookieEntry = cookies.find(cookie => cookie.startsWith(name + '='));
// Return just the value portion if found, otherwise null
return cookieEntry ? decodeURIComponent(cookieEntry.split('=')[1]) : null;
}
// Phase 1 & 2: Theme Functions
function storeTheme(themeName) {
// Set cookie with 24 hour expiration for persistence
const expirationDays = 1;
const expirationDate = new Date();
expirationDate.setDate(expirationDate.getDate() + expirationDays);
// Format cookie with name=value;expires=date
document.cookie = `themeName=${themeName};expires=${expirationDate.toUTCString()};path=/`;
}
function restoreTheme() {
const savedTheme = getCookie('themeName');
if (savedTheme) {
setTheme(savedTheme);
}
}
function clearTheme() {
// Set expiration to past date to remove cookie
document.cookie = 'themeName=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/';
}
// Phase 3: Display Name Functions
function storeName(displayName) {
// Store as session cookie (no expiration date)
document.cookie = `displayName=${encodeURIComponent(displayName)};path=/`;
}
function restoreName() {
const savedName = getCookie('displayName');
if (savedName) {
setInputValue('display-name', savedName);
}
}
function clearName() {
// Set expiration to past date to remove cookie
document.cookie = 'displayName=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/';
}
The getCookie utility function solves a key challenge when working with multiple cookies. Think of document.cookie like a cookie jar - when you look inside, all cookies are mixed together. This function helps us find the specific cookie we want:
function getCookie(name) {
const cookies = document.cookie.split('; ');
const cookieEntry = cookies.find(cookie => cookie.startsWith(name + '='));
return cookieEntry ? decodeURIComponent(cookieEntry.split('=')[1]) : null;
}
This function:
The theme storage implements persistent cookies that survive browser restarts:
function storeTheme(themeName) {
const expirationDays = 1;
const expirationDate = new Date();
expirationDate.setDate(expirationDate.getDate() + expirationDays);
document.cookie = `themeName=${themeName};expires=${expirationDate.toUTCString()};path=/`;
}
This creates a cookie that:
The display name uses session cookies that clear when the browser closes:
function storeName(displayName) {
document.cookie = `displayName=${encodeURIComponent(displayName)};path=/`;
}
Notice the lack of an expiration date - this makes it a session cookie.
Cookie management like this is commonly used for:
To thoroughly test this implementation:
When working with cookies, watch out for:
Cookies must be properly formatted with name=value pairs and semicolon separators. Common issues include:
When multiple cookies exist, you need proper parsing logic to extract specific values. Our getCookie helper function handles this cleanly.
While this implementation is safe for preferences, remember: