forked from dcompute/Zepto-Cookie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zepto.cookie.js
50 lines (40 loc) · 1.79 KB
/
zepto.cookie.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Zepto.cookie plugin
//
// Copyright (c) 2010, 2012
// @author Klaus Hartl (stilbuero.de)
// @author Daniel Lacy (daniellacy.com)
//
// Dual licensed under the MIT and GPL licenses:
// http://www.opensource.org/licenses/mit-license.php
// http://www.gnu.org/licenses/gpl.html
;(function($){
$.extend($.fn, {
cookie : function (key, value, options) {
var days, time, result, decode
// A key and value were given. Set cookie.
if (arguments.length > 1 && String(value) !== "[object Object]") {
// Enforce object
options = $.extend({}, options)
if (value === null || value === undefined) options.expires = -1
if (typeof options.expires === 'number') {
days = (options.expires * 24 * 60 * 60 * 1000)
time = options.expires = new Date()
time.setTime(time.getTime() + days)
}
value = String(value)
return (document.cookie = [
encodeURIComponent(key), '=',
options.raw ? value : encodeURIComponent(value),
options.expires ? '; expires=' + options.expires.toUTCString() : '',
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.secure ? '; secure' : ''
].join(''))
}
// Key and possibly options given, get cookie
options = value || {}
decode = options.raw ? function (s) { return s } : decodeURIComponent
return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null
}
})
})(Zepto)