skip to Main Content

I am trying to learn cookies in js but the name=value pairs aren’t disappearing from the console when I set a well past due expiration date. I think it has something to do with the error code with the CSP. Does anybody know how to fix it?

the js code:

document.cookie = "firstName=Bob; expires=Sat, 1 2025 23:59:59 EST; path=/";
document.cookie = "lastName=Roberts; expires=Sat, 1 2000 23:59:59 EST; path=/";
console.log(document.cookie);

console line 1: firstName=Bob; lastName=Roberts

1 issue: Content Security Policy of your site blocks the use of ‘eval’ in JavaScript`

I tried changing time zones, and dates, and updating VSC.

2

Answers


  1. You won’t see this kind of client-side created cookie expire, because it would only ever expire when the browser is (re)loading a page from the cookie’s domain.

    Cookies are sent to the server as an identifying alphanumeric value that is the key to some data value. I for one don’t understand why you’d want to add one in JavaScript.

    Login or Signup to reply.
  2. Think your date strings are just incorrect, this deletes those cookies:

    document.cookie = "firstName=; expires=Sat, 1 Jan 2000 23:59:59 EST; path=/";
    document.cookie = "lastName=; expires=Sat, 1 Jan 2000 23:59:59 EST; path=/";
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search