View RSS Feed

jscheuer1

Compact Cookie Unit

Rate this Entry
Cookies are often confusing. Much of the confusion can be eliminated if you know that all cookies are strings. If you want to store an array, function, number, or an object in a cookie, you must convert it to a string before storage and upon retrieval change it back into the object/array/function/number that it was. Cookies are therefore best used for string storage and retrieval. This unit doesn't pretend to eliminate all of the possible confusions that can arise from "when and where in my code is the best place to set/read/delete the cookie". However, it does greatly simplify those actions:

Code:
var cook = {
	set: function(n, v, d){ // cook.set takes (name, value, optional_persist_days) - defaults to session if no days specified
		if(d){var dt = new Date(); 
			dt.setDate(dt.getDate() + d);
		d = '; expires=' + dt.toGMTString();}
		document.cookie = n + '=' + escape(v) + (d || '') + '; path=/';
	},
	get: function(n){ // cook.get takes (name)
		var c = document.cookie.match('(^|;)\x20*' + n + '=([^;]*)');
		return c? unescape(c[2]) : null;
	},
	kill: function(n){ // cook.kill takes (name)
		cook.set(n, '', -1);
	},
	killall: function(){ // cook.killall takes no parameters
		var cookies = document.cookie.split(';'), i = cookies.length - 1;
		for (i; i > -1; --i){
			cook.kill(cookies[i].split('=')[0]);
		}
	}
};
Usage is indicated in the comments. For example, to set a cookie named 'bob' to a value of 'your uncle' to persist for 2 days:

Code:
cook.set('bob', 'your uncle', 2);
To read said cookie (it will return its string value if set, null if not set) - no semicolon follows the example because it will most often be a part of an if or variable assignment statement.

Code:
cook.get('bob')
To wit:

Code:
if(cook.get('bob'))
  do one thing
else
  do another thing
To erase said cookie:

Code:
cook.kill('bob');
That's it!

Edit: July 2010 - changed the name of the object to cook from cookie as this was giving errors in some browsers. Added killall function.

Submit "Compact Cookie Unit" to del.icio.us Submit "Compact Cookie Unit" to StumbleUpon Submit "Compact Cookie Unit" to Google Submit "Compact Cookie Unit" to Digg

Updated 07-10-2010 at 03:59 PM by jscheuer1

Categories
Post a JavaScript

Comments

  1. Nile's Avatar
    Wow, I'm pretty sure I'm going to use this. Thanks! Credit required?
  2. ddadmin's Avatar
    Nice compact code!
  3. jscheuer1's Avatar
    @Nile,

    No credit required, but it would be nice to at least mention the forums. However, since this code is a distillation of many other's work including my own, you should not in any way take credit for it yourself.

    @DD,

    Thanks!
  4. vmars316's Avatar
    Thanks for the code.
    Its a bit confusing to me (newbie) because of the new format (set: function),
    Rather than what i am used to (function SetCookie).
    What are the advantages of writing it your (not newbie) way?
    Thanks...vmars316
  5. jscheuer1's Avatar
    Quote Originally Posted by vmars316
    What are the advantages of writing it your (not newbie) way?
    Less pollution of the global scope.