Stefan Gabos web developer extraordinaire
Zebra_Cookie, an extremely small jQuery plugin for writing, reading and deleting cookies
-
Latest version1.0.1released onNovember 03, 2011
- 1. Overview
- 2. Requirements
- 3. How to use
- 4. Methods
- 5. Download
- 6. Changelog
- 7. Comments
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
Requirements
Zebra_Cookie requires jQuery 1.0+
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');
});
Methods
| destroy |
Removes a cookie from the browser.
where name is the name of the cookie to remove;
Returns TRUE on success or FALSE otherwise |
|
| read |
Reads the value of a cookie.
where name is the name of the cookie to read;
Returns the value of the requested cookie or null if the cookie doesn’t exist. |
|
| write |
Sets a cookie in the browser.
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.
Returns TRUE if the cookie was successfully set, or FALSE otherwise. |
Download
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 thas 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:
- jQuery Plugin Boilerplate
- Zebra_Accordion, a tiny accordion jQuery plugin
- Zebra_Cookie, an extremely small jQuery plugin for writing, reading and deleting cookies
- Zebra_Datepicker, a lightweight datepicker jQuery plugin
- Zebra_Dialog, a lightweight dialog box jQuery plugin
- Zebra_Tooltips, a lightweight tooltips jQuery plugin
- Zebra_TransForm, a tiny jQuery plugin for replacing checkboxes, radio buttons and selects
Changelog
Click on a version to expand/collapse information.
- 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




I am a 32 year old web developer working from Bucharest, Romania. I am coding since I was 14 and I am extremely passionate about it. For the server side of things I use PHP/MySQL while on the front-end I write valid HTML 5, nice CSS and lots of JavaScript code using jQuery.
Thanks for this script. I struggled with https://github.com/carhartl/jquery-cookie and got nowhere but yours worked with a minimum of fuss. Good work!
Reply