2

I am creating cookies using Javascript as below

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    } else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

While Firefox and Chrome create a mcoo cookie for 7 days and mdes cookie for SESSION, Internet Explorer creates only the mcoo cookie. What am I missing here?

    createCookie('mcoo', hash, 7);
    createCookie('mdes', hash);

Update

As a workaround I have changed the days * 24 to 6, and changed the expiration day of mcoo to 28 (so this is 1 week) and put an expiration daty to mdes 1 (6 hours). But I want per session.

EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179
  • your `expires` variable is being set within the `if` and `else` blocks. They are out of scope when you are trying to create the cookie (`undefined`). – ps2goat Aug 19 '14 at 22:06
  • 1
    @ps2goat Eh.. no. JavaScript's `var` is function-level scope, not block level. – Jeremy J Starcher Aug 19 '14 at 22:11
  • 1
    @JeremyJStarcher, I see. Doesn't it still make sense to declare a variable at the top before going into the if blocks, if the variable is scoped and used throughout the function? – ps2goat Aug 19 '14 at 22:18
  • @ps2goat Those words start religious wars, fire and brimstone... but I will say that many would agree with you including the venerated or hated Douglas Crockford. – Jeremy J Starcher Aug 19 '14 at 22:20

1 Answers1

4

As you find out... IE work's different (again, extrange eh?)

giving a ZERO value to expire, will just work as expected in firefox& chrome but not in IE because in IE, the cookie will expire immediately as soon as it is set.

So...

FF & CR:

document.cookie = name + "=" + value + " expires=0; path=/";

...To make this work in IE, you need to remove the expire parameter and leave just like this:

IE:

document.cookie = name + "=" + value + " ; path=/";

Update

I will probably use something like:
edit: tested and working on IE11 && CR36

function createCookie(name, value, days) {

    var c_date,
    c_name = name + "=" + value + ";",
    c_expi = "",
    c_path = "path=/";

    if (days > 0) {
        c_date = new Date();
        c_date.setTime(c_date.getTime() + (days * 24 * 60 * 60 * 1000));
        c_expi = "expires=" + c_date.toGMTString() + ";";
    }

    // create the cookie
    document.cookie = c_name + c_expi + c_path;

}

Then, call the function:

// normal cookie expire in 7 days
createCookie('mcoo', hash, 7);
// session cookie
createCookie('mdes', hash, 0);

Source: Setting Session Only Cookie via JavaScript
More: Session only cookies with Javascript

Community
  • 1
  • 1
gmo
  • 8,860
  • 3
  • 40
  • 51
  • I see, thank you. How can I change my code in order to allow me set cookies for 7 days and session cross browsers? – EnexoOnoma Aug 19 '14 at 22:18
  • Use the one for `IE` for all... giving no `expire parameter`, will make a `session cookie` – gmo Aug 19 '14 at 22:20
  • Oh! look what I found, same explanation & same source XD... http://stackoverflow.com/questions/14196671/session-only-cookies-with-javascript – gmo Aug 19 '14 at 22:21
  • but if I replace the current with the IE suggestion, how can I create cookies with 7 days expiration? – EnexoOnoma Aug 19 '14 at 22:36