Junior
How can you check if cookies are enabled in the browser?
sobes.tech AI
Answer from AI
You can try to set a test cookie and then read it. If reading is successful, cookies are enabled.
try {
// Attempt to set a test cookie
document.cookie = "testcookie=testvalue; samesite=Strict";
// Attempt to read the test cookie
const cookieEnabled = document.cookie.includes("testcookie=testvalue");
// Delete the test cookie
document.cookie = "testcookie=; expires=Thu, 01 Jan 1970 00:00:00 UTC; samesite=Strict";
return cookieEnabled;
} catch (e) {
// If an error occurs during setting or reading, cookies are likely disabled
return false;
}