A first party cookie differs from a session cookie in that the cookie itself is stores in a file on your visitor's computer rather than in their browser. By storing the cookie outside the browser the cookie can retain data after the browser has closed that can be used when the person next visits the site.
Unlike where we just want the cookie kept until the browser is closed, we do not have any particular event that will indicate how long a first party needs to be retained for and so we need to specify it. This is done by adding an expires value when we set the cookie and it is the presence of this value that makes this a first party cookie rather than a session cookie.
The expires value needs to be a date in a very specific format that will indicate exactly when the cookie is to be considered to have expired. In this example we look at how we can easily get JavaScript to create this expiry date in the correct format for us with a function that allows us to specify a retention period in days and have that converted into the appropriate date.
writeCookie = function(cname, cvalue, days) {
var dt, expires;
dt = new Date();
dt.setTime(dt.getTime()+(days*24*60*60*1000));
expires = "; expires="+dt.toGMTString();
document.cookie = cname+"="+cvalue+expires;
}
