/****************************************************************************** cookies.js, developed by Charlton D. Rose for Inquiry.Com Permission is granted to modify and use this source code, free of charge, provided that this header remains with the code, unmodified. ******************************************************************************/ /****************************************************************************** function write_cookie (name, value); function write_cookie (name, value, path); write_cookie creates a cookie with the name, value, and path given in the parameters. If no path is supplied, write_cookie uses the document's default cookie path. Each cookie is set with an expiration time of 1 year. ******************************************************************************/ function write_cookie (name, value, path) { //alert("write") // Build the expiration date string: var expiration_date = new Date (); expiration_date . setYear (expiration_date . getYear () + 1); expiration_date = expiration_date . toGMTString(); // Build the set-cookie string: var cookie_string = escape (name) + "=" + escape (value) + "; expires=" + expiration_date; if (path != null) cookie_string += "; path=" + path; // Create/update the cookie: document . cookie = cookie_string; } /****************************************************************************** function read_cookie (key) function read_cookie (key, skips) read_cookie searches through the current document's cookie string (i.e., the concatenation of all cookies readable by the current document) for a cookie whose name is identical to key. If read_cookie finds a matching cookie, it returns a string containing the value of cookie. If read_cookie cannot find a match, it returns null instead. An optional skips parameter may be supplied if there is a need to select among multiple cookies with the same name. If a skips parameter is supplied, read_cookie will skip that many occurrences of matching cookies and then return the next one it finds, or null if there aren't any more. ******************************************************************************/ function read_cookie (key, skips) { // Set skips to 0 if parameter was omitted: if (skips == null) skips = 0; // Get cookie string and separate into individual cookie phrases: var cookie_string = "" + document . cookie; var cookie_array = cookie_string . split ("; "); // Scan for desired cookie: //alert(cookie_array) for (var i = 0; i < cookie_array . length; ++ i) { var single_cookie = cookie_array [i] . split ("="); if (single_cookie . length < 2) continue; var name = unescape (single_cookie [0]); var value = unescape (single_cookie [1]); // Return cookie if found: if (key == name && skips -- == 0) return value; } // Cookie was not found: return null; } /****************************************************************************** delete_cookie (name) delete_cookie (name, path) delete_cookie deletes the cookie with the specified name and path. If no path is given, the default cookie path for the current document is used instead. If the name and path do not match an existing cookie, delete_cookie has no effect. ******************************************************************************/ function delete_cookie (name, path) { // Build expiration date string: var expiration_date = new Date (); expiration_date . setYear (expiration_date . getYear () - 1); expiration_date = expiration_date . toGMTString(); // Build set-cookie string: var cookie_string = escape (name) + "=; expires=" + expiration_date; if (path != null) cookie_string += "; path=" + path; // Delete the cookie: document . cookie = cookie_string; } /****************************************************************************** delete_all_cookies () delete_all_cookies (path) delete_all_cookies deletes all cookies matching the specified path. If no path is supplied, the document's default cookie path is used. ******************************************************************************/ function delete_all_cookies (path) { // Get cookie string and separate into individual cookie phrases: var cookie_string = "" + document . cookie; var cookie_array = cookie_string . split ("; "); // Try to delete each cookie: for (var i = 0; i < cookie_array . length; ++ i) { var single_cookie = cookie_array [i] . split ("="); if (single_cookie . length != 2) continue; var name = unescape (single_cookie [0]); delete_cookie (name, path); } }