Xi 张熹
Xi 张熹

Reputation: 11071

Why my cookie is lost when the browser is closed? (Expires set by javascript)

I am trying to set a permanent cookie by JavaScript. I have something like:

document.cookie = name+'='+value+";expires="+(new Date(2019, 1, 1)).toUTCString();

It does not work. The cookie disappears when the browser is closed. Why is that? :(

Upvotes: 2

Views: 4268

Answers (2)

coder
coder

Reputation: 13250

Try this

function setCookie(key, value) {  
   var expires = new Date();  
   expires.setTime(expires.getTime() + 31536000000); //1 year  
   document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();  
   }  

I have just set to 1 Year and you can change it to your convinience.

Upvotes: 2

Sunjay Varma
Sunjay Varma

Reputation: 5115

I believe you literally have to set a number of days. So like 10000 or however long you want it to last.

Upvotes: 1

Related Questions