httpdocs/jquery.cookie.js
author Eugen Sawin <sawine@me73.com>
Mon, 20 Feb 2012 23:41:50 +0100
changeset 2 b25c62d210f5
permissions -rw-r--r--
Added pid file writing.
sawine@0
     1
/*!
sawine@0
     2
 * jQuery Cookie Plugin
sawine@0
     3
 * https://github.com/carhartl/jquery-cookie
sawine@0
     4
 *
sawine@0
     5
 * Copyright 2011, Klaus Hartl
sawine@0
     6
 * Dual licensed under the MIT or GPL Version 2 licenses.
sawine@0
     7
 * http://www.opensource.org/licenses/mit-license.php
sawine@0
     8
 * http://www.opensource.org/licenses/GPL-2.0
sawine@0
     9
 */
sawine@0
    10
(function($) {
sawine@0
    11
    $.cookie = function(key, value, options) {
sawine@0
    12
sawine@0
    13
        // key and at least value given, set cookie...
sawine@0
    14
        if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
sawine@0
    15
            options = $.extend({}, options);
sawine@0
    16
sawine@0
    17
            if (value === null || value === undefined) {
sawine@0
    18
                options.expires = -1;
sawine@0
    19
            }
sawine@0
    20
sawine@0
    21
            if (typeof options.expires === 'number') {
sawine@0
    22
                var days = options.expires, t = options.expires = new Date();
sawine@0
    23
                t.setDate(t.getDate() + days);
sawine@0
    24
            }
sawine@0
    25
sawine@0
    26
            value = String(value);
sawine@0
    27
sawine@0
    28
            return (document.cookie = [
sawine@0
    29
                encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
sawine@0
    30
                options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
sawine@0
    31
                options.path    ? '; path=' + options.path : '',
sawine@0
    32
                options.domain  ? '; domain=' + options.domain : '',
sawine@0
    33
                options.secure  ? '; secure' : ''
sawine@0
    34
            ].join(''));
sawine@0
    35
        }
sawine@0
    36
sawine@0
    37
        // key and possibly options given, get cookie...
sawine@0
    38
        options = value || {};
sawine@0
    39
        var decode = options.raw ? function(s) { return s; } : decodeURIComponent;
sawine@0
    40
sawine@0
    41
        var pairs = document.cookie.split('; ');
sawine@0
    42
        for (var i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
sawine@0
    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
sawine@0
    44
        }
sawine@0
    45
        return null;
sawine@0
    46
    };
sawine@0
    47
})(jQuery);