Set, Get, and Remove
The Cookies namespace exposes three static functions that makes reading, writing, and removing cookies easy.
Fit.Cookies.Set("MyCookie", "Hello world", 24*60*60); // Set cookie (expires after 86400 seconds - 24 hours)
alert(Fit.Cookies.Get("MyCookie")); // Display value stored in MyCookie
Fit.Cookies.Remove("MyCookie"); // Remove cookie
The Set function takes four arguments: Cookie name, Cookie value, optional expiration time in seconds (defaults to 0 = expires when browser is closed), and optional path which defaults to "/" which makes the cookie accessible to the entire domain.
The Get function takes only the name of the cookie.
The Remove function takes two arguments: The name of the cookie and optionally the alternative path used to set the cookie.
Cookies set using Fit.Cookie.Set are globally accessible on the entire domain on which they were created. This creates two problems:
1) It increases the possibility that two applications overwrite each others cookies if using the same names.
2) It increases data sent to the server as all cookies available to a web application is sent to the server on every request.
Fit.UI makes it possible to create a "cookie container" to reduce the risk of overwriting cookies from other applications, and keep cookies isolated to a specific portion of the site or web application.
// Cookies set using this cookie container will only be
// accessible to the current location on the domain,
// and the prefix "MyApp" ensures that internally all
// cookies are prefixed with "MyApp" making it even
// less likely that cookies from other applications will
// cause conflicts.
// Cookies set using the cookie container is only made
// available to the current application path, e.g. /sites/demo.
var cookies = new Fit.Cookies();
// Complex applications may use a lot of cookies, so
// adding an internal prefix to all cookie names will
// reduce the risk of overwriting cookies from different
// parts of the sytem if the same cookie name has been used.
cookies.Set("MyCookie", "Hello world", 24*60*60); // Set cookie (expires after 86400 seconds - 24 hours)
alert(cookies.Get("MyCookie")); // Display value stored in MyCookie
cookies.Remove("MyCookie"); // Remove cookie
Be aware that the cookie container does NOT provide any added security. If an iFrame can be embedded on any page on the domain, it can be pointed to any given location from where the cookies can be read using iframe.contentDocument.cookie. Only an alternative domain or subdomain can be used to prevent this due to the same origin policy