Not signed in (Sign In)
    •  
      CommentAuthorstubblechin
    • CommentTimeOct 30th 2007 edited
     permalink

    For some reason, after upgrading to Leopard, cookies that should have been persistent were being deleted when I restarted Safari. It turns out Safari's Javascript engine no longer automatically coerces Date objects to strings when you're setting cookies. Instead, it silently throws away your date, turning what you thought was a persistent cookie into a session cookie that is deleted when Safari is closed. The solution is to manually convert your cookie expiration date to a string, if you've been using Date objects.

    Old code that no longer works:

    function setCookie (name, value, expires, path, domain, secure) {
        var curCookie = name + "=" + encodeURIComponent(value) + (expires ? "; expires=" + expires : "") + (path ? "; path=" + path : "") + (domain ? "; domain=" + domain : "") + (secure ? "secure" : "");
        document.cookie = curCookie;
    }
    

    Working code:

    function setCookie (name, value, expires, path, domain, secure) {
        var curCookie = name + "=" + encodeURIComponent(value) + (expires ? "; expires=" + expires.toGMTString() : "") + (path ? "; path=" + path : "") + (domain ? "; domain=" + domain : "") + (secure ? "secure" : "");
        document.cookie = curCookie;
    }
    

    (The only difference is that I'm calling the method toGMTString() on the variable expires).

    So if you've got Mac users on your site, and you're setting any persistent cookies with Javascript, make sure you're doing this! Really, there's no reason not to, because it doesn't hurt on any browser, it only helps.