Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate key is getting set in Javascript Cookie

My issue is that the JS cookie gets set with two "viewmore" keys. Here "viewmore" is set to true and false. Can anyone help diagnose please!?

 > document.cookie
    "viewmore=true; SESSID=fjs0fmojglrih7; viewmore=false; user=1"

Shouldn't the "viewmore" key be getting overwritten and not duplicated with a different value?

Code that didn't work:

document.cookie = "viewmore=false";
document.cookie = "viewmore=true";

Code that worked: needed an expiry set

var now = new Date();
now.setTime(now.getTime() + 1 * 3600 * 1000);

document.cookie = "viewmore=false; expires=" + now.toUTCString() + "; path=/";
document.cookie = "viewmore=true; expires=" + now.toUTCString() + "; path=/";
like image 625
Andres Avatar asked May 11 '16 23:05

Andres


1 Answers

See above post for working code that fixed my problem.

Seems like it should be considered a good habit to set an expiry with cookies.

like image 104
Andres Avatar answered Sep 22 '22 14:09

Andres