Site hosted by Angelfire.com: Build your free website today!
 
MyersDaily Directories
Message Board | Lycos | JavaScripts.com
Your Pages | What's Up | Contents | Bottom of Page
[Home][Archive]

Copy-and-Paste Cookie Functions



function makeCookie(Name,Value,Expiry,Path,Domain,Secure) {
   //Bunch of arguments

if (Expiry != null && !isNaN(Expiry)) {
   //if you want to save the cookie

var datenow = new Date();
   //get a date

datenow.setTime(datenow.getTime() + Math.round(86400000*Expiry));
   //mutiply the number to make it represent days

Expiry = datenow.toGMTString();
   //convert to GMT time

}
   //ends that. And now...

Expiry = (Expiry) ? '; expires='+Expiry : '';
   //has an expiration?

Path = (Path)?'; path='+Path:'';
   //has a path?

Domain = (Domain) ? '; domain='+Domain : '';
   //has a domain?

Secure = (Secure) ? '; secure' : '';
   //Secure?

document.cookie = Name + '=' + escape(Value) + Expiry + Path + Domain + Secure;
   //Make the cookie!

}

function readCookie(Name) {
   //Your name goes here!

var cookies = ' ' + document.cookie;
   //Copy your cookies

if (cookies.indexOf(' ' + Name + '=') == -1) return null;
   //Whoops, no cookie!

var start = cookies.indexOf(' ' + Name + '=') + (Name.length + 2);
   //Jump to start of cookie

var finish = cookies.substring(start,cookies.length);
   //Get a count from the cookies

finish = (finish.indexOf(';') == -1) ? cookies.length : start + finish.indexOf(';');
   //Find end of cookie

return unescape(cookies.substring(start,finish));
   //Here's your cookie! ( Sorry, no chocolate chips. :-)

}

// End of cookie functions (Joseph K. Myers 1999-2000 <e_mayilme@hotmail.com>)

/*
Syntax rules:

***********************    Making the Cookie / Reading the Cookie   ***********************

   A. Name
       1. No characters besides a-z, A-Z, numerals, dash and underscore.
       2. Probably don't want to use an especially long name.
   B. Value
       1. Here's the actual content.
       2. Anything goes here up to 4000 characters.
   C. Expiry (This is what shines!)
       1. Type in the NUMBER OF DAYS to save the cookie or:
       2. Leave this out and the cookie will expire during the current session.
   D. Path
       1. Sets a restriction on the portion of the server allowed to access a cookie.
       2. Use a slash "/" to allow any page from your site to obtain the cookie.
   E. Domain
       1. Sets a domain as in MAIL.yahoo.com  ( EMPHASIS ADDED!!! ).
       2. By default this would be set to the same as the cookie's origin domain.
   F. Secure
       1. If you want access to the cookie ONLY during a secure connection.
       2. Normally you'd want this to protect sensitive information.
       
   You would type this...
   
makeCookie('friendly_cookie', 'Mr. Smith is tall.', 18, '/');

   ...to make a cookie with a NAME of friendly_cookie, a VALUE of "Mr. Smith is tall.", an
   EXPIRATION of 18 days later, and having the most accessible PATH of /.

       
   You would type this...
   
var YOUR_VARIABLE_NAME = readCookie('friendly_cookie');

   ...to set the variable of YOUR_VARIABLE_NAME to the value of the cookie with a NAME of
   friendly_cookie or to null if there was no cookie available named friendly_cookie.

       
   You would type this...
   
makeCookie('toughcookie', somevariable, null, '/directory/pages/', 'mail.yahoo.com', true);

   ...to make a cookie with a NAME of toughcookie, a VALUE of the variable somevariable, an
   EXPIRATION of current session, and having a PATH of /directory/pages/ at the DOMAIN of
   mail.yahoo.com, and only accessible in SECURE connections.

       
   Typing the following...
   
makeCookie('cookie-name', '', 0, '/same/path/as/before', 'at-the-same-domain');

   ...would delete a cookie with a NAME of cookie-name. The path and domain must be specified
   just as before since two cookies by the same name with different paths can coexist on one domain.


   
   Smile! :-) This code is free for the taking. Consider it to be in the public domain. Feel free
   to contact me at <e_mayilme@hotmail.com> or visit my website which has even more cool
   scripts at <https://www.angelfire.com/yt/jmyers/>.
   
   Note: You'll likely want to remove most comments from this script to shorten download times.
*/

//These cookie functions written 1999-2000 by Joseph K. Myers
 
Enter keywords...  
Amazon.com logo Featured! | Send to a friend!
Home | Top | Feedback | Site Index | Best Photo
“MyersDaily” created by Joseph K. Myers 1999-2000. Free hosting by Angelfire!!
Note: This site's URL is: https://www.angelfire.com/yt/jmyers   Site Updated On: October 6, 2000
Terms-of-Service