/** * Copyright (C) 2002-2003, CodeHouse.com. All rights reserved. * CodeHouse(TM) is a registered trademark. * * THIS SOURCE CODE MAY BE USED FREELY PROVIDED THAT * IT IS NOT MODIFIED OR DISTRIBUTED, AND IT IS USED * ON A PUBLICLY ACCESSIBLE INTERNET WEB SITE. * * CodeHouse.com JavaScript Library Module: Cookie Utility Class * * You can obtain this script at http://www.codehouse.com */ function CJL_CookieUtil(name, duration, path, domain, secure) { this.affix = ""; this.name = name; if( duration ) { var date = new Date(); var curTime = new Date().getTime(); date.setTime(curTime + (1000 * 60 * duration)); this.affix = "; expires=" + date.toGMTString(); } if( path ) { this.affix += "; path=" + path; } if( domain ) { this.affix += "; domain=" + domain; } if( secure ) { this.affix += "; secure=" + secure; } this.getValue = function() { var m = document.cookie.match(new RegExp("(" + this.name + "=[^;]*)(;|$)")); return m ? m[1] : null; } this.cookieExists = function() { return this.getValue() ? true : false; } this.expire = function() { var date = new Date(); date.setFullYear(date.getYear() - 1); document.cookie=this.name + "=noop; expires=" + date.toGMTString(); } this.setSubValue = function(key, value) { var ck = this.getValue(); var newCookieStr = ''; //escape key and value to avoid "&" and "=" tokens key = this.urlEncode(key); value = this.urlEncode(value); //If cookie exists if(ck){ ck = ck.replace(new RegExp('^' + this.name + '='),''); var keyValueCol = ck.split('&'); for(var i=0; i < keyValueCol.length; i++){ var keyValue = keyValueCol[i].split('='); if(keyValue[0] != key){ newCookieStr += keyValueCol[i] + '&'; } } if(value != '') newCookieStr += key + '=' + value + '&'; //Removes the last '&' if(newCookieStr != '') newCookieStr = newCookieStr.substr(0,newCookieStr.length-1); }else if(value != ''){ //Add cookie if value is set newCookieStr = key + '=' + value; } if(newCookieStr != '') document.cookie = this.name + '=' + newCookieStr + this.affix; else this.expire(); } this.getSubValue = function(key) { var ck = this.getValue(); if( ck ) { ck = ck.replace(new RegExp(this.name + '='), ''); key = this.urlEncode(key); var m = ck.split('&'); for(var i = 0; i < m.length; i++){ if(m[i].indexOf('=') >= 0){ var keyValue = m[i].split('='); if(keyValue[0] == key) return this.urlDecode(keyValue[1]); } } } return ''; } this.urlEncode = function(str){ var SAFECHARS = "0123456789" + // Numeric "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic "abcdefghijklmnopqrstuvwxyz"; var HEX = "0123456789ABCDEF"; var plaintext = str; var encoded = ""; for (var i = 0; i < plaintext.length; i++ ) { var ch = plaintext.charAt(i); if (ch == " ") { encoded += "+"; // x-www-urlencoded, rather than %20 } else if (SAFECHARS.indexOf(ch) != -1) { encoded += ch; } else { var charCode = ch.charCodeAt(0); if (charCode > 255) { encoded += "+"; } else { encoded += "%"; encoded += HEX.charAt((charCode >> 4) & 0xF); encoded += HEX.charAt(charCode & 0xF); } } } // for return encoded; } this.urlDecode = function(str){ var HEXCHARS = "0123456789ABCDEFabcdef"; var encoded = str; var plaintext = ""; var i = 0; while (i < encoded.length) { var ch = encoded.charAt(i); if (ch == "+") { plaintext += " "; i++; } else if (ch == "%") { if (i < (encoded.length-2) && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) { plaintext += unescape( encoded.substr(i,3) ); i += 3; } else { plaintext += "%[ERROR]"; i++; } } else { plaintext += ch; i++; } } return plaintext; } }