3ad834d8 by Marty Penner

Importing Cookie directory from javascript.git

1 parent f18a686a
Cookie @ 4c4d02ff
1 Subproject commit 4c4d02ff6f1e226778ee969fdaa4ec33b716deaa
1 /**
2 * A Singleton class to create, read and delete cookies
3 * @author Chris Boden
4 * @version 0.3
5 */
6 Cookie = new function() {
7 /**
8 * Create a cookie for current domain
9 * @param {String} name The name of the cookie by reference
10 * @param {String} value The value of the cookie to be stored
11 * @param {Integer} days The Number of days until the cookie expires
12 * @returns null
13 */
14 this.create = function(name, value, days) {
15 var expires = '';
16 if (days) {
17 var date = new Date();
18 date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
19 expires = '; expires= ' + date.toGMTString();
20 }
21
22 document.cookie = name + '=' + value + expires + '; path=/';
23 }
24
25 /**
26 * Get the value of a cookie for this domain
27 * @param {String} name The name of the cookie
28 * @returns A string if cookie exists null if not
29 */
30 this.read = function read(name) {
31 var nameEQ = name + '=';
32 var ca = document.cookie.split(';');
33 for(var i = 0; i < ca.length; i++) {
34 var c = ca[i];
35 while (c.charAt(0)==' ') {
36 c = c.substring(1,c.length);
37 }
38
39 if (c.indexOf(nameEQ) == 0) {
40 return c.substring(nameEQ.length,c.length);
41 }
42 }
43
44 return null;
45 }
46
47 /**
48 * Erases a cookie from the domain
49 * @param {String} name Name of the cookie to erase
50 * @returns null
51 */
52 this.erase = function(name) {
53 Cookie.create(name, '', -1);
54 }
55 }
1 A simple class to creat, read and erase cookies