//***************************************************************************** //* CCookie.js //***************************************************************************** //* Copyright 2005-2006 by GalaSoft Laurent Bugnion //***************************************************************************** //* Project Name : CCookie //* Target Hardware : PC //* Target : Mozilla, Internet explorer 5+ //* Language/Compiler : ECMAScript V3 //* Author : Laurent Bugnion (LBu) //***************************************************************************** //* Description: //* Cookie functions. //* Last Debug version: D0001 //* //* Code is provided without guarantees. //* //* The reader is allowed to use this code and modify it if needed. The original code //* is posted under www.galasoft.ch/myjavascript/CCookie/ccookie.txt //* //* A reference to www.galasoft.ch is appreciated, but not compulsory. //***************************************************************************** //* Revision History: //* 15.04.2005 Lbu : Created. //***************************************************************************** // Create namespace if ( !window.gslb ) { window.gslb = function() { } } //***************************************************************************** //* Constructor *************************************************************** //***************************************************************************** /// /// Dummy constructor. Use gslb.CCookie.methodName() /// for "static" methods. /// gslb.CCookie = function() { } //***************************************************************************** //* Imports ******************************************************************* //***************************************************************************** // using none //***************************************************************************** //* Constants ***************************************************************** //***************************************************************************** /// /// Path for the cookies. When set to '/', makes the cookie valid /// for the whole domain. /// gslb.CCookie.PATH = '/'; //***************************************************************************** //* Static methods ************************************************************ //***************************************************************************** /// /// Creates a Date object set to the current date/time plus a number of years, months, days, etc... /// /// The number of years to add to the current Date/Time. /// The number of years to add to the current Date/Time. /// The number of years to add to the current Date/Time. /// The number of years to add to the current Date/Time. /// The number of years to add to the current Date/Time. /// The number of years to add to the current Date/Time. /// The number of years to add to the current Date/Time. /// The current date/time plus the number of years, months... specified /// in the parameters. gslb.CCookie.getDateTimeFromNow = function( iYears, iMonths, iDays, iHours, iMinutes, iSeconds, iMilliseconds ) { var dtNow = new Date(); dtNow.setFullYear( dtNow.getFullYear() + iYears ); dtNow.setMonth( dtNow.getMonth() + iMonths ); dtNow.setDate( dtNow.getDate() + iDays ); dtNow.setHours( dtNow.getHours() + iHours ); dtNow.setMinutes( dtNow.getMinutes() + iMinutes ); dtNow.setSeconds( dtNow.getSeconds() + iSeconds ); dtNow.setMilliseconds( dtNow.getMilliseconds() + iMilliseconds ); return dtNow; } /// /// Extracts a cookie value. /// Note: This method should not be used externally, but rather /// as a utility for other methods. /// /// Offset to extract the value from the string. /// The extracted value. gslb.CCookie.getValue = function( iOffset ) { var iEndstr = document.cookie.indexOf ( ";", iOffset ); if ( iEndstr == -1 ) { iEndstr = document.cookie.length; } return unescape( document.cookie.substring( iOffset, iEndstr ) ); } /// /// Gets a cookie identified by its name. /// /// Cookie's name. /// The cookie's value, or null if no corresponding cookie /// is found. gslb.CCookie.getCookie = function( strName ) { var strArg = strName + "="; var iArgLength = strArg.length; var iCookieLength = document.cookie.length; var i = 0; while ( i < iCookieLength ) { var j = i + iArgLength; if ( document.cookie.substring( i, j ) == strArg ) { return gslb.CCookie.getValue( j ); } i = document.cookie.indexOf( " ", i ) + 1; if ( i == 0 ) { break; } } return null; } /// /// Sets a cookie. /// /// Cookie's name. /// Cookie's value. /// A Date object, which specifies until when the /// cookie is valid. If this parameter is left blank, or null, the cookie /// is a Session cookie. /// Cookie's path. If this parameter is left blank, /// the cookie is valid for the whole path (inclusive subfolders). /// Cookie's domain. If this parameter is left blank, /// nothing is specified. /// If true, the cookie is secure. If this parameter /// is left blank, nothing is specified. gslb.CCookie.setCookie = function( strName, strValue, dtExpires, strPath, strDomain, bSecure ) { document.cookie = strName + "=" + escape ( strValue ) + ( ( dtExpires == null ) ? "" : ( "; expires=" + dtExpires.toGMTString() ) ) + ( ( strPath == null ) ? "" : ( "; path=" + gslb.CCookie.PATH ) ) + ( ( strDomain == null ) ? "" : ( "; domain=" + strDomain ) ) + ( ( bSecure == true ) ? "; secure" : "" ); } /// /// Deletes a cookie identified by its name. /// /// Cookie's name. gslb.CCookie.deleteCookie = function( strName ) { var dtExpiration = new Date(); dtExpiration.setTime ( dtExpiration.getTime() - 1 ); // This cookie is history gslb.CCookie.setCookie( strName, "", dtExpiration, gslb.CCookie.PATH ); } /// /// Tests if persistent cookies are enabled. /// /// True if persistent cookies are enabled, false otherwise. gslb.CCookie.testPersistentEnabled = function() { var dtExpiration = new Date(); // Valid one minute. dtExpiration.setTime( dtExpiration.getTime() + ( 60 * 1000 ) ); gslb.CCookie.setCookie( "testCookie", "OK", dtExpiration ); var strTest = gslb.CCookie.getCookie( "testCookie" ); if ( strTest == "OK" ) { return true; } else { return false; } } /// /// Tests if session cookies are enabled. /// /// True if session cookies are enabled, false otherwise. gslb.CCookie.testSessionEnabled = function() { gslb.CCookie.setCookie( "testCookieSession", "OK" ); var strTest = gslb.CCookie.getCookie( "testCookieSession" ); if ( strTest == "OK" ) { return true; } else { return false; } }