Zebra_Cookie, an extremely small jQuery plugin for writing, reading and deleting cookies

Get the latest updates on this jQuery plugin via RSS

Handling cookies can be a daunting task using plain JavaScript, and since jQuery doesn’t natively include functions for handling cookies, I’ve created Zebra_Cookie – an extremely small (~500 bytes minified) jQuery plugin for writing, reading and deleting cookies.

Features

  • easily write, read and delete cookies
  • very simple and intuitive syntax, blending perfectly with jQuery
  • extremely small: with around 500 bytes when minified is probably the smallest and most optimized jQuery plugin for handling cookies

Top

Requirements

Zebra_Cookie requires jQuery 1.0+

Top

How to use

Load the latest version of jQuery either from a local source, or from a CDN

<script type="text/javascript" src="path/to/jQuery.js"></script>

Load the Zebra_Cookie plugin

<script type="text/javascript" src="path/to/zebra_cookie.js"></script>

Usage

// inside the DOM-ready function
// a "cookie" object will be available in jQuery’s namespace
// the object exposes 3 methods that you can use to write, read and delete cookies
$(document).ready(function() {
 
    // create a session cookie (expires when the browser closes)
    $.cookie.write('cookie_name', 'cookie_value');
 
    // create a cookie that expires in 1 day
    $.cookie.write('cookie_name', 'cookie_value', 24 * 60 * 60);
 
    // read a cookie’s value
    // following the examples above, this should return "cookie_value"
    $.cookie.read('cookie_name');
 
    // the "read" method returns null if the cookie doesn’t exist
    $.cookie.read('non_existing_cookie_name');
 
    // delete the cookie
    $.cookie.destroy('cookie_name');
 
});

Top

Methods

destroy  

Removes a cookie from the browser. 

$.cookie.destroy(name)

where

name is the name of the cookie to remove;

$(document).ready(function() {
 
    // create a session cookie (expires when the browser closes)
    $.cookie.write('cookie_name', 'cookie_value');
 
    // delete the cookie
    $.cookie.destroy('cookie_name');
 
});

Returns TRUE on success or FALSE otherwise

read  

Reads the value of a cookie. 

$.cookie.read(name)

where

name is the name of the cookie to read;

$(document).ready(function() {
 
    // create a session cookie (expires when the browser closes)
    $.cookie.write('cookie_name', 'cookie_value');
 
    // read a cookie’s value
    // following the examples above, this should return "cookie_value"
    $.cookie.read('cookie_name');
  
});

Returns the value of the requested cookie or null if the cookie doesn’t exist.

write  

Sets a cookie in the browser. 

$.cookie.write(name, value, [expire = 0], [path = /], [domain = false], [secure = false])

where

name: The name of the cookie;

value: The value to set;

expire: (Optional) The life time of the cookie, in seconds. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).

path: (Optional) The path on the server in which the cookie will be available on. If set to “/”, the cookie will be available within the entire domain. If set to ‘/foo/’, the cookie will only be available within the /foo/ directory and all subdirectories such as /foo/bar/ of domain. If omitted, it will be set to “/”.

domain: (Optional) The domain that the cookie will be available on. To make the cookie available on all sub-domains of example.com, domain should be set to to “.example.com”. The . (dot) is not required but makes it compatible with more browsers. Setting it to “www.example.com” will make the cookie available only in the www sub-domain.

secure: (Optional) Indicates whether cookie information should only be transmitted over a HTTPS connection. Default is FALSE.

$(document).ready(function() {
 
    // create a session cookie (expires when the browser closes)
    $.cookie.write('cookie_name', 'cookie_value');
 
    // create a cookie that expires in 1 day
    $.cookie.write('cookie_name', 'cookie_value', 24 * 60 * 60);
 
});

Returns TRUE if the cookie was successfully set, or FALSE otherwise.

Top 

Download

version 1.0.2
If you find this library to be useful to you, you can support the author by donating a small amount via PayPal:

Zebra_Cookie is distributed under the LGPL.

In plain English, this means that you have the right to view and to modify the source code of this software, but if you modify and distribute it, you are required to license your copy under a LGPL-compatible license, and to make the entire source code of your derivation available to anybody you distribute the software to.

You also have the right to use this software together with software that has different licensing terms (including, but not limited to, commercial and closed-source software), and distribute the combined software, as long as state that your software contains portions licensed under the LGPL license, and provide information about where the LGPL licensed software can be downloaded.

If you distribute copies of this software you may not change the copyright or license of this software.


You may also like:

Top

Changelog

Click on a version to expand/collapse information.

version 1.0.2 (December 17, 2013)
  • added “use strict” statement;
  • added integration with Grunt
  • the library is now available as a Bower package
version 1.0.1 (November 03, 2011)
  • fixed a small bug where the “destroy” method was not returning anything
  • minor additions to the source code comments
version 1.0 (October 30, 2011)
  • initial release

Top

1 response to “Zebra_Cookie, an extremely small jQuery plugin for writing, reading and deleting cookies”

Follow the comments via RSS