// Start of script.
function dbstream(a1) {
var db = '';
for (var k in a1) if (a1[k] || a1[k] == '') db += '&' + escape(k) + '=' + escape(a1[k]);
return db.substring(1);
}
function dbparse(a1,a2) {
var db = {};
if (!a1) return db;
a1 = a1.split(a2 || '&');
for (var k in a1) {
k = a1[k].split('=');
db[unescape(k[0])] = unescape(k[1]);
}
return db;
}
function setcookie(a1,a2,a3) {
if (!a1) return;
function daystring(A1) {
var time = new Date();
time.setTime(time.getTime() + (86400000 * A1))
return time.toGMTString();
}
document.cookie = escape(a1) + '=' + escape(a2) + (a3 &&
( isNaN(a3.expires) ? '' : '; expires=' + daystring(a3.expires) ) +
( a3.path ? '; path=' + a3.path : '' ) +
( a3.domain ? '; domain=' + a3.domain : '' ) +
( a3.secure ? '; secure' : '' ) || '');
}
function getcookie(a1){
a1 = escape(a1);
var tie = ' ' + document.cookie;
var sta = tie.indexOf(' ' + a1 + '=');
if (sta == -1) return null;
var end = tie.indexOf(';',sta);
return (end != -1) ? unescape(tie.substring(sta + 2 + a1.length,end)) : unescape(tie.substring(sta + 2 + a1.length));
}
function tiecookies(a1,a2,a3) {
if (!a1 || !a2) return;
var tied = '';
for (var k in a2) {
tied += '&' + escape(k);
setcookie(a1 + '-' + k,a2[k],a3);
}
if (tied) setcookie(a1,tied.substring(1),a3);
}
function untiecookies(a1) {
var tied = getcookie(a1);
if (tied == null) return tied;
var ties = {};
if (!tied) return ties;
tied = tied.split('&');
for (var i=0; i<tied.length; i++) {
tied[i] = unescape(tied[i]);
ties[tied[i]] = getcookie(a1 + '-' + tied[i]);
}
return ties;
}
function killcookies(a1) {
tiecookies(a1,untiecookies(a1),{ expires:0 });
}
function tiecookie(a1,a2,a3) {
if (!a1 || !a2) return;
setcookie(a1,dbstream(a2),a3);
}
function untiecookie(a1) {
var ties = getcookie(a1);
if (ties == null) return ties;
return dbparse(ties);
}
function killcookie(a1) {
setcookie(a1,'',{ expires:0 });
}
function verifycookie() {
var myname = 'cookie-support';
var myvalu = 'testing-your-cookie-support';
setcookie(myname,myvalu);
return getcookie(myname) == myvalu;
}
function dbcookie() {
return dbparse(document.cookie,'; ');
}
/*
This general purpose cookie/cookie-database JavaScript module is designed with the ability to set cookies using any name and value, read the cookies back, tie an object to a cookie or cookies, and verify a page's cookie availability. Please note that the cookie databases are not truly tied to their variables -- changes made to the variable in JavaScript are not updated in the cookies as well (unless the database is re-set).
To easily check whether cookie read/write privileges are available, the verifycookie() function can be used. See this example.
if ( verifycookie() ) {
// You can have cookies, so do your stuff.
// whatever...
} else {
// You can't, so do something else.
}
Simple cookie creation is provided by the setcookie() function. The function has three arguments, and the last is optional. Since the third argument is supplied as an object, you can omit any or all values. The expiration is set to the number of days for the cookie data to persist. See the following usage examples.
setcookie(cookie_name,cookie_value,
{ expires:expiration,
path:path,
domain:domain,
secure:true OR false }
)
// Your available options
setcookie('cookie-name','hello');
// Will expire this session.
setcookie('Yes, this is a valid name.','anything', { expires:1 });
// Will expire in 1 day.
setcookie('whatever.','stuff goes here', { path:'/area/' });
// Path of /area/
setcookie('Mr. Bluejay!','...', { domain:'a.b.com', secure:1 });
// Domain a.b.com, secure access only.
To get the value of any cookie, use the getcookie() function. It requires a single argument which specifies a cookie's name. When the requested cookie is not found, a value of null is returned. See the following example.
getcookie(COOKIE_NAME);
// returns value of cookie COOKIE_NAME or null
Always remember that cookies cannot distinguish between text strings, Boolean values, or numeric floats. Any value returned is a simple string. A cookie made containing a null, true/false, or other non-textual value will be converted to text.
To permanently delete a cookie, use the killcookie() function and supply the name of the cookie to be deleted. It makes no difference whether the cookie specified exists or not. An example is shown.
killcookie('cookie-name');
Now for the exciting part. Extended cookie database capability is now available using the dbstream(), dbparse(), tiecookie(), untiecookie(), tiecookies(), untiecookies(), and killcookies() functions.
To put it simply, dbstream() converts a standard JavaScript object[...] (or associative array) into an inline text stream. Since the resulting value is plain text it can be stored properly into a cookie. Note that all values are converted into text, with the exception that values of null or 0 are not indexed (converted to undefined).
Then, to read back the values, dbparse() parses the text string back into a JavaScript object[...]. A special feature of dbparse() is you can use an optional second argument to change the default record separator '&'. (Similar to changing the $/ variable in Perl.) This argument is passed to the function as a string or a regular expression (e.g. 'string' -or- /regexp.../).
The dbcookie() function makes use of this second argument to construct a cookie object from the document.cookie string. When you assign the result of dbcookie() to a variable it loads all cookies into memory. No arguments are used. See the example here.
var mycookies = dbcookie();
Now mycookies.cookie_name or mycookies['hello there'] is the same as using getcookie('cookie_name') or getcookie('hello there') respectively. Once cookies are loaded into a variable with dbcookie(), they can be accessed from the variable much faster than by using the getcookie() function. However, cookie data contained within the variable is only reflective of the cookies present when the variable was constructed.
The tie/untie cookie functions use the dbstream() and dbparse() functions to store more than one value within a single cookie. Call tiecookie() with two or three arguments. The first argument gives the cookie its name, the second must be a valid JavaScript object, and the third optionally adds an expiration, path, etc. to the cookie. The third argument is used just as with the setcookie() function.
// A JavaScript Object:
{ property1:value1, property2:value2 };
e.g. tiecookie('database-name', { data object }, { (optional cookie settings) });
The untiecookie() function finds the data cookie requested, parses its value back to an object, then returns the object. It uses one argument. If the cookie cannot be found, the value returned is null, but no error is created.
e.g. var saved_database = untiecookie('database-name') || {};
Note that || {} was typed after requesting the untied cookie. This is done since a null value will be returned if the cookie is not found. If the cookie cannot be found, trying to use the returned value as an object will create an error (since the value returned is null). So the line above, translated to English, says, "Either get me the database 'database-name', or give me an empty object."
Use killcookie() to delete a database created with tiecookie(), supplying the name of the database as the argument.
The tiecookies() and untiecookies() functions work identically to the tiecookie() and untiecookie() functions, respectively, with one important distinction and another subtle difference. The important distinction between the two is the database size factor. The tie/untie cookies functions establish a multi-cookie database rather than one tied to a single cookie. This makes these functions more efficient, and capable of much greater storage. The downside of this feature is that they could quickly hog your domain's cookie allotment. This is less of a problem with Explorer than Netscape since Netscape limits each domain to 20 cookies.
Also, since your database is tied to multiple cookies, you can't delete a database with the killcookie() function (without leaving a bad mess of cookies behind), but instead must use the killcookies() function. Running killcookies() with your database name will delete all the cookies used by the database.
Database Usage:
var mytie = { user:'Mary P. Jones', date:'Sat May 20 21:25:30 CST 2000' }
tiecookie('data-list',mytie); // You could also use tiecookies for this,
cookie_data = untiecookie('data-list') || {}; // and then untiecookies for this.
// cookie_data.user + '\r' + cookie_data.date;
// equals
// Mary P. Jones
// Sat May 20 21:25:30 CST 2000
*/
/*
Questions or Comments? Email them to the author: "Joseph K. Myers" <e_mayilme@hotmail.com>.
Include all pertinent information. A response is not guaranteed.
Free use for any non-restricted type website is granted.
Do not remove this comment section.
Script Author: Joseph K. Myers <e_mayilme@hotmail.com>
Script Source: MyersDaily <https://www.angelfire.com/yt/jmyers/>
# |- Script Specifications -| #
# language: javascript1.2 Version: 1.3.1 Release Date: May 22, 2000
*/
// End of script.