├── test.html
├── server.js
├── jquery.cookie.js
├── test.js
└── README.md
/test.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | jquery.cookie Test Suite
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/server.js:
--------------------------------------------------------------------------------
1 | var http = require('http'),
2 | url = require('url'),
3 | path = require('path'),
4 | fs = require('fs');
5 |
6 | http.createServer(function(request, response) {
7 | var uri = url.parse(request.url).pathname,
8 | filename = path.join(process.cwd(), uri);
9 |
10 | fs.readFile(filename, 'binary', function(err, file) {
11 | if (err) {
12 | response.writeHead(500, { 'Content-Type': 'text/plain' });
13 | response.write(err + '\n');
14 | response.end();
15 | return;
16 | }
17 |
18 | response.writeHead(200);
19 | response.write(file, 'utf-8');
20 | response.end();
21 | });
22 | }).listen(8124, '0.0.0.0');
23 |
24 | console.log('Test suite at http://0.0.0.0:8124/test.html');
25 |
--------------------------------------------------------------------------------
/jquery.cookie.js:
--------------------------------------------------------------------------------
1 | /*!
2 | * jQuery Cookie Plugin
3 | * https://github.com/carhartl/jquery-cookie
4 | *
5 | * Copyright 2011, Klaus Hartl
6 | * Dual licensed under the MIT or GPL Version 2 licenses.
7 | * http://www.opensource.org/licenses/mit-license.php
8 | * http://www.opensource.org/licenses/GPL-2.0
9 | */
10 | (function($) {
11 | $.cookie = function(key, value, options) {
12 |
13 | // key and at least value given, set cookie...
14 | if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
15 | options = $.extend({}, options);
16 |
17 | if (value === null || value === undefined) {
18 | options.expires = -1;
19 | }
20 |
21 | if (typeof options.expires === 'number') {
22 | var days = options.expires, t = options.expires = new Date();
23 | t.setDate(t.getDate() + days);
24 | }
25 |
26 | value = String(value);
27 |
28 | return (document.cookie = [
29 | encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
30 | options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
31 | options.path ? '; path=' + options.path : '',
32 | options.domain ? '; domain=' + options.domain : '',
33 | options.secure ? '; secure' : ''
34 | ].join(''));
35 | }
36 |
37 | // key and possibly options given, get cookie...
38 | options = value || {};
39 | var decode = options.raw ? function(s) { return s; } : decodeURIComponent;
40 |
41 | var pairs = document.cookie.split('; ');
42 | for (var i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
43 | if (decode(pair[0]) === key) return decode(pair[1] || ''); // IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, thus pair[1] may be undefined
44 | }
45 | return null;
46 | };
47 | })(jQuery);
48 |
--------------------------------------------------------------------------------
/test.js:
--------------------------------------------------------------------------------
1 | var before = {
2 | setup: function () {
3 | cookies = document.cookie.split('; ')
4 | for (var i = 0, c; (c = (cookies)[i]) && (c = c.split('=')[0]); i++) {
5 | document.cookie = c + '=; expires=' + new Date(0).toUTCString();
6 | }
7 | }
8 | };
9 |
10 |
11 | module('read', before);
12 |
13 | test('simple value', 1, function () {
14 | document.cookie = 'c=v';
15 | equal($.cookie('c'), 'v', 'should return value');
16 | });
17 |
18 | test('empty value', 1, function () {
19 | $.cookie('c', '');
20 | equal($.cookie('c'), '', 'should return value');
21 | });
22 |
23 | test('not existing', 1, function () {
24 | equal($.cookie('whatever'), null, 'should return null');
25 | });
26 |
27 | test('decode', 1, function () {
28 | document.cookie = encodeURIComponent(' c') + '=' + encodeURIComponent(' v');
29 | equal($.cookie(' c'), ' v', 'should decode key and value');
30 | });
31 |
32 | test('raw: true', 1, function () {
33 | document.cookie = 'c=%20v';
34 | equal($.cookie('c', { raw: true }), '%20v', 'should not decode');
35 | });
36 |
37 |
38 | module('write', before);
39 |
40 | test('String primitive', 1, function () {
41 | $.cookie('c', 'v');
42 | equal(document.cookie, 'c=v', 'should write value');
43 | });
44 |
45 | test('String object', 1, function () {
46 | $.cookie('c', new String('v'));
47 | equal(document.cookie, 'c=v', 'should write value');
48 | });
49 |
50 | test('value "[object Object]"', 1, function() {
51 | $.cookie('c', '[object Object]');
52 | equal($.cookie('c'), '[object Object]', 'should write value');
53 | });
54 |
55 | test('number', 1, function() {
56 | $.cookie('c', 1234);
57 | equal($.cookie('c'), '1234', 'should write value');
58 | });
59 |
60 | test('return value', 1, function () {
61 | equal($.cookie('c', 'v'), 'c=v', 'should return written cookie string');
62 | });
63 |
64 | test('raw: true', 1, function () {
65 | equal($.cookie('c', ' v', { raw: true }).split('=')[1],
66 | ' v', 'should not encode');
67 | });
68 |
69 |
70 | module('delete', before);
71 |
72 | test('delete', 2, function () {
73 | document.cookie = 'c=v';
74 | $.cookie('c', null);
75 | equal(document.cookie, '', 'should delete with null as value');
76 |
77 | document.cookie = 'c=v';
78 | $.cookie('c', undefined);
79 | equal(document.cookie, '', 'should delete with undefined as value');
80 | });
81 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # jquery.cookie
2 |
3 | A simple, lightweight jQuery plugin for reading, writing and deleting cookies.
4 |
5 | ## Installation
6 |
7 | Include script *after* the jQuery library (unless you are packaging scripts somehow else):
8 |
9 |
10 |
11 | ## Usage
12 |
13 | Create session cookie:
14 |
15 | $.cookie('the_cookie', 'the_value');
16 |
17 | Create expiring cookie, 7 days from then:
18 |
19 | $.cookie('the_cookie', 'the_value', { expires: 7 });
20 |
21 | Create expiring cookie, valid across entire page:
22 |
23 | $.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });
24 |
25 | Read cookie:
26 |
27 | $.cookie('the_cookie'); // => 'the_value'
28 | $.cookie('not_existing'); // => null
29 |
30 | Delete cookie by passing null as value:
31 |
32 | $.cookie('the_cookie', null);
33 |
34 | *Note: when deleting a cookie, you must pass the exact same path, domain and secure options that were used to set the cookie.*
35 |
36 | ## Options
37 |
38 | expires: 365
39 |
40 | Define lifetime of the cookie. Value can be a `Number` (which will be interpreted as days from time of creation) or a `Date` object. If omitted, the cookie is a session cookie.
41 |
42 | path: '/'
43 |
44 | Default: path of page where the cookie was created.
45 |
46 | Define the path where cookie is valid. *By default the path of the cookie is the path of the page where the cookie was created (standard browser behavior).* If you want to make it available for instance across the entire page use `path: '/'`.
47 |
48 | domain: 'example.com'
49 |
50 | Default: domain of page where the cookie was created.
51 |
52 | secure: true
53 |
54 | Default: `false`. If true, the cookie transmission requires a secure protocol (https).
55 |
56 | raw: true
57 |
58 | Default: `false`.
59 |
60 | By default the cookie is encoded/decoded when creating/reading, using `encodeURIComponent`/`decodeURIComponent`. Turn off by setting `raw: true`.
61 |
62 | ## Changelog
63 |
64 | ## Development
65 |
66 | - Source hosted at [GitHub](https://github.com/carhartl/jquery-cookie)
67 | - Report issues, questions, feature requests on [GitHub Issues](https://github.com/carhartl/jquery-cookie/issues)
68 |
69 | Pull requests are very welcome! Make sure your patches are well tested. Please create a topic branch for every separate change you make.
70 |
71 | ## Authors
72 |
73 | [Klaus Hartl](https://github.com/carhartl)
74 |
--------------------------------------------------------------------------------