My favorites | Sign in
Project Logo
                
Search
for
Updated Jun 04, 2008 by burakgursoyx
Labels: Phase-Implementation
HTTPCookies  

NAME

HTTP.Cookies - JavaScript class for manipulating cookies


SYNOPSIS

   var cookie    = new HTTP.Cookies;
   var password  = cookie.read('password');
   var lastvisit = cookie.read('lastvisit');
   cookie.write( 'lastvisit', 1079383075, '+1y' );
   cookie.remove( 'password' );

DESCRIPTION

HTTP.Cookies is a class for ''HTTP Cookies'' manipulation. It defines three object methods to read, write and remove cookies. Implementation is somehow similar to the Perl module "CGI.pm"' s "cookie()" method.

There is an internal cache system for fast access. But the cache will be reset if you alter ("write()" or "remove()") cookies. So, you can use the same "HTTP.Cookies" object anywhere in the page.


METHODS

read NAME

Reads the cookie named "NAME" and returns it's value or an empty string upon failure.

write NAME, VALUE [, EXPIRES, PATH, DOMAIN, SECURE]

Creates a new cookie with "NAME" and "VALUE". "NAME" can include non-alphanumeric characters, but you are discouraged to use such cookie names.

Optional "EXPIRES" value sets the cookie lifetime.

Expire date format: you can use negative or positive numbers combined with "s", "m", "h", "d", "w", "M", "y" or you can use "now" to expire as soon as possible. Meanings:

    s   = second
    m   = minute
    h   = hour
    d   = day
    w   = week
    M   = month
    y   = year
    now = immediately

For a session cookie; pass "-1" as the expires value.

Example:

   cookie.write('mycookie', 'testing', '+1h');

"mycookie" will expire in one hour.

Optional parameter "PATH" can be used to define the path for which the HTTP cookie is valid:

   // Only accessible in the cgi-bin path
   cookie.write('mycookie', 'testing', '+1h', '/cgi-bin');
   // Globally accessible
   cookie.write('mycookie', 'testing', '+1h', '/');

Optional parameter "DOMAIN" can be used to define the domain for which the HTTP cookie is valid:

   // Can be accessed from www.openjsan.org, master.openjsan.org, planet.openjsan.org, etc.
   cookie.write('mycookie1', 'testing', '+1h', '/', '.openjsan.org');
   // Can only be accessed from www.openjsan.org
   cookie.write('mycookie2', 'testing', '+1h', '/', 'www.openjsan.org');

Optional parameter "SECURE" can be used to make it a secure cookie. Secure cookies can only be used with HTTPS protocol. They'll be invisible under HTTP protocol. This is a bool value:

   cookie.write('mycookie', 'testing', '+1h', '/', 'www.openjsan.org', 1);
   cookie.write('mycookie', 'testing', '+1h', '/', 'www.openjsan.org', true);
   cookie.write('mycookie', 'testing', '+1h', '/', 'www.openjsan.org', 'secure');

remove NAME [, PATH, DOMAIN, SECURE]

Deletes/removes the named cookie from the client. See "write" for the meaning of the parameters.

obliterate

Will remove/delete any accessible cookie.

names

Returns an array consisting of available cookies' names:

   document.write( "Available Cookie names are ", cookie.names().join(', ') );

ERROR HANDLING

Methods will return null if an error occurs. But the error can also be diplayed in a message box if the error level is not altered.

HTTP.Cookies.ERRORLEVEL

If set to "1" (default value), then fatal errors will be displayed in a message box. Any other value will silence the errors.


CAVEATS

Under NS 4.x and MSIE, when you delete the cookies, their names ''may'' not be deleted. The value of the cookie will be erased (will return "undefined"), but you ''may'' need to close the browser window to erase the cookie completely. I'm not sure if there is a workaround for IE. And I haven't tested this with IE7 yet.


BUGS

Contact the author if you find any.

This library is tested under Windows XP Professional SP2 with:


SEE ALSO

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Predefined_Functions:escape_and_unescape_Functions, http://developer.mozilla.org/en/docs/DOM:document.cookie, http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-8747038, http://www.ietf.org/rfc/rfc2965.txt.


AUTHOR

Burak Gürsoy, <burak@cpan.org>


COPYRIGHT

Copyright 2005-2008 Burak Gürsoy. All rights reserved.


LICENSE

This library is free software; you can redistribute it and/or modify it under the terms of the "Artistic License": http://dev.perl.org/licenses/artistic.html.


Sign in to add a comment
Hosted by Google Code