/*** (C)Scripterlative.com Info: http://scripterlative.com?xcookie These instructions may be removed but not the above text. Please notify any suspected errors in this text or code, however minor. The XCookie object provides a conveniently packaged suite of cookie handling functions, whose function and usage are described below. Installation ~~~~~~~~~~~~ Save this file or text as 'xcookie.js' and copy it to a suitable folder relating to your web pages. Within the section of all documents that will use the script, insert the following: (If xcookie.js resides in a different folder, include the relative path) Configuration ~~~~~~~~~~~~~ None. Description of Functions and Usage ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ bump( cookieName, increment, duration, path, domain, secure ) Creates or increments the value of a numeric cookie. The value is set to or incremented by the numeric value specified by 'increment'. Negative and floating point values may be used. Returns the newly-stored value or "" on failure. Example of a one-line visit counter: // Increment the value of 'mysiteVisits' by 1 and set lifetime to 30 days. // If 'mysiteVisits' doesn't exist, create it with a value of 1. document.write( "Number of visits: " + XCookie.bump('mysiteVisits', 1, 30) ); // or to count and display once per session if( !XCookie.exists( 'mySession' ) ) { document.write( "Number of visits: " + XCookie.bump('mysiteVisits', 1, 30) ); XCookie.set( 'mySession', true, 0 ); } clear( cookieName, path ) Deletes a specified cookie XCookie.clear( 'lastPageViewed' ); Returns boolean true on success. clearAll( path ) Clears all cookies on the specified path, or current if not specified. Returns the number of cookies successfully deleted. enabled() Returns the boolean status of cookie support on the client. if( !XCookie.enabled() ) alert("Please enable cookie support") exists( cookieName ) Returns the boolean existence status of a specified cookie. if( XCookie.exists('noSound') ) playSound=false; read( cookieName ) Returns the value of a specified cookie, or "" on failure. if( (v = XCookie.read('uservisits')) != "" ) document.write("Number of previous visits: " + v); refresh( cookieName, days, path, domain, secure ) Refreshes the lifetime of a specified cookie, preserving its current value. // Reset the lifetime of cookie 'userID' to 30 days XCookie.refresh('userID', 30); set( cookieName, cookieValue, duration, path, domain, secure ) //Creates a cookie called 'userName' containing the data 'J Smith' with a duration of 10 days and optional path: set("userName", "J Smith", 10); set("userName", "J Smith", 10, '/login'); //Creates a cookie as above but with a duration of 5 minutes (300 seconds): set("userName", "J Smith", "secs = 300"); Meaning of Parameters ~~~~~~~~~~~~~~~~~~~~~ cookieName: The name of the cookie. cookieValue: The value to be stored. duration: The lifetime of the cookie. This can be either: a) A number to specify the number of days the cookie is to exist. b) A string of the form "secs = n", where n is the lifetime in seconds. To create a session cookie, specify 0. increment: The amount by which the numeric decimal integer value stored will be incremented. path: (Optional) Represents the highest folder level at which the cookie is accessible. The default value is the current folder. domain: (Optional) The domain on which the cookie will be readable, if not the current. secure: (Optional) set to true (without quotes). This code is free, however if you wish to make a donation to encourage further development of scripts, you may do so at www.scripterlative.com. ***** DO NOT EDIT BELOW THIS LINE ******/ var XCookie = /*2843295374657068656E204368616C6D657273*/ { /** Free download with instructions at http://scripterlative.com?xcookie **/ escFunc:encodeURIComponent || escape, unescFunc:decodeURIComponent || unescape, read:function(cookieName) { var cValue=""; if( typeof document.cookie != 'undefined' ) cValue = ( cValue = document.cookie.match( "(^|;)\\s*"+this.escFunc(cookieName)+'=([^;]+);?') ) ? cValue[ 2 ] : "" ; return this.unescFunc( cValue ); }, set:function( cName, cValue, cLifetime, path, domain, secure ) { var duration, useSeconds = false, cs = "", expDate = new Date(); if( typeof path != 'string' || !/\S/.test( path ) ) path = ''; if( typeof cLifetime == 'string' && (duration = cLifetime.match(/^\s*secs\s*\=\s*(\d+)\s*$/i)) ) { duration = Number( duration[ 1 ] ); useSeconds = true; } else duration = Number( cLifetime ); if( typeof duration != 'number' ) alert( "Error: Duration value set incorrectly for cookie '" + cName + "'" ); else if( /[\;\=]/.test( cName ) ) alert("Illegal character in cookie name"); else { if( !useSeconds ) expDate.setDate( expDate.getDate() + duration ); else expDate.setSeconds( expDate.getSeconds() + duration ); cs = this.escFunc( cName ) + "=" + this.escFunc( cValue ) + ';' ; if( path ) cs += ';path=' + path; if( duration ) cs += ";expires=" + expDate.toGMTString(); if( domain ) cs += ';domain=' + domain; if(secure === true) cs += ';secure' ; window.document.cookie = cs; } return this.read( cName ); }, refresh:function( cName, duration, path, domain, secure ) { var val = this.read( cName ); if(val !== "") this.set( cName, val, duration, path, domain, secure ); return this.read( cName ); }, clear:function( cName, path, domain ) { return !this.set( cName, 0, -1, path, domain ); }, clearAll:function( path ) { var rslt, rxp = /(^|;)\s*([^=]+)=[^;]*/g, cString, count=0; if( typeof( cString = window.document.cookie ) == 'string' ) while( ( rslt = rxp.exec( cString ) ) ) count += ( this.set( this.unescFunc( rslt[ 2 ] ), 0, -1, path ) === "" ) ? 1 : 0; return count; }, bump:function( cName, increment, duration, path, domain, secure ) { var v; return this.set( cName, !isNaN( v = parseFloat( this.read( cName ), 10) ) ? v + increment : increment, duration, path, domain, secure ); }, exists:function( cName ) { return( Boolean( typeof document.cookie == 'string' && document.cookie.match( '(^|;)\\s*' + cName + '=' ) ) ); }, enabled:function() { var rv = false, cString; if( typeof document.cookie != 'undefined' ) { for( var i = 0; this.exists( "XCookieSupport" + i ); i++ ) ; rv = !!this.set( cString = "XCookieSupport" + i, 'OK', 1 ); this.clear( cString ); } return rv; } }; /*** End of listing ***/