- Provide a cookie manager that allows developers to set the domain, path, expiration, and security of their cookies on the fly.
- Check to see if a given cookie exists by name.
- Provide a list of all cookies and their keys using a simple interface.
- Read and write cookies and their values.
- Read and write cookie keys and their values (i.e., multiple-value cookies!)
- Remove cookies by name.
It is assumed that the person using this code has a general understanding of how cookies work, and is seeking a more robust cookie management solution.
I created this manager as a response to the frustrations I dealt with while trying to manage cookies from client code, specifically the lack of keys (multiple-value cookies). The core read/write of the basic cookie was modeled after the cookie reader/write found here.
I took this and extended it to allow for the use of keys and (hopefully) make it more flexible and powerful for everyday users.
Using the Code
Simply instantiate a new
Cookies object and then use the various read/write functions to read and write to your cookies and keys.
var cookieManager = new Cookies();
var cookieManagerWithOptions = new Cookies('mysite.com', '/tools', '10/31/2012', false);
cookieManager.writeKey('customer', 'name', 'Jeff Smith');
cookieManager.writeKey('customer', 'dob', '07/15/1991');
cookieManager.domain(); cookieManager.path(); cookieManager.expires(); cookieManager.secure();
Points of Interest
Sadly, when many of these developers are tasked with doing anything that isn't handled by their chosen library, their first response is always 'let's look for a plugin' and while I agree that code reuse is fantastic, at some point developers need to learn how to think critically and solve problems without relying on oftentimes bulky third party software.
- Version 1.0 - Added sources files, testing harness, and documentation
- Version 1.1 - Updated source files (testing harness, core) and updated documentation
- Version 1.2 - Removed a superfluous reference to an unversioned testing harness script, updated core to include ability to fetch path, expiration, and the secure status of the manager
- Version 1.3 - Addressed bug that was causing failures when attempting to write a key with a blank value