Problem Writing Cookies in Safari

Discussion in 'Web Design and Development (archive)' started by crees!, Mar 3, 2004.

  1. macrumors 68000

    crees!

    Joined:
    Jun 14, 2003
    Location:
    MD/VA/DC
    #1
    So I'm having this is problem where Safari will only write cookies that I make if they just have a name and value. If I set the expiration or path it will not write the cookie. I know my code is correct because I'm taking it straight from a web book. Plus I checked Safari's Developer FAQ's and they had the same example. So I don't know what gives.


    This works:

    var cartName = "AppleCart";

    var expiration = new Date();
    expiration.setMinutes(expiration.getMinutes() + 120);

    function writeCookie(value)
    {
    document.cookie = cartName + "=" + escape(value);

    if (document.cookie != "")
    alert("Cookie has been written");
    else
    alert("Cookie has not been saved");
    }


    This does not work:

    function writeCookie(value)
    {
    document.cookie = cartName + "=" + escape(value) + "; expires=" + expiration.toGMTString();

    if (document.cookie != "")
    alert("Cookie has been written");
    else
    alert("Cookie has not been saved");
    }
     
  2. thread starter macrumors 68000

    crees!

    Joined:
    Jun 14, 2003
    Location:
    MD/VA/DC
    #2
    Well I realized Safari doesn't like using setMinutes(), but instead setDate() but correctly write the cookie. Still, this is interesting. But then maybe I'm just a nub. ;)
     
  3. thread starter macrumors 68000

    crees!

    Joined:
    Jun 14, 2003
    Location:
    MD/VA/DC
    #3
    And now finally reading up further I see that Minutes are from 0-59.. so setting it + 120 sort of breaks that doesn't it...
     
  4. macrumors 601

    Westside guy

    Joined:
    Oct 15, 2003
    Location:
    The soggy part of the Pacific NW
    #4
    I've ran into that before - Safari's date and time handling seem to be a lot pickier than with Mozilla or IE. I was passing the date to my cookie routines and then using a Date() object to convert it; other than with Safari I didn't have to be too careful about the format of my input string.
     

Share This Page