Error setting cookies with expiry date with javascript "When browsing session ends"

Asked

Viewed 139 times

1

I have the following code:

function createCookie(name,value) {

    name = "testeeee";
    value = "conteudo do teseeee";
    var date = new Date();
    date.setTime(date.getTime()+(720*60*60*1000));
    var expires = "; expires="+date.toLocaleString();

    alert(date.toLocaleString());
    alert(name+"="+value+expires+"; path=/");
    document.cookie = name+"="+value+expires+"; path=/";
  }

But in the browser appears like this:

inserir a descrição da imagem aqui

I try every way to set the end date of it to expire, but only in that in the browser.

  • Do you want the cookie to stay until how long? By my calculation you want 12h or I’m wrong?

1 answer

1


The cookie expiration date must be GMT. Use the toGMTString function as below:

function createCookie(name,value) {

    name = "testeeee";
    value = "conteudo do teseeee";
    var date = new Date();
    date.setTime(date.getTime()+(720*60*60*1000));
    var expires = "; expires="+date.toGMTString();

    alert(date.toLocaleString());
    alert(name+"="+value+expires+"; path=/");
    document.cookie = name+"="+value+expires+"; path=/";
  }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.