]> git.uio.no Git - usit-rt.git/blame - share/static/js/jquery.cookie.js
Merge branch 'master' of git.uio.no:usit-rt
[usit-rt.git] / share / static / js / jquery.cookie.js
CommitLineData
af59614d
MKG
1/*!
2 * jQuery Cookie Plugin v1.3.1
3 * https://github.com/carhartl/jquery-cookie
4 *
5 * Copyright 2013 Klaus Hartl
6 * Released under the MIT license
7 */
8(function (factory) {
9 if (typeof define === 'function' && define.amd && define.amd.jQuery) {
10 // AMD. Register as anonymous module.
11 define(['jquery'], factory);
12 } else {
13 // Browser globals.
14 factory(jQuery);
15 }
16}(function ($) {
17
18 var pluses = /\+/g;
19
20 function raw(s) {
21 return s;
22 }
23
24 function decoded(s) {
25 return decodeURIComponent(s.replace(pluses, ' '));
26 }
27
28 function converted(s) {
29 if (s.indexOf('"') === 0) {
30 // This is a quoted cookie as according to RFC2068, unescape
31 s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
32 }
33 try {
34 return config.json ? JSON.parse(s) : s;
35 } catch(er) {}
36 }
37
38 var config = $.cookie = function (key, value, options) {
39
40 // write
41 if (value !== undefined) {
42 options = $.extend({}, config.defaults, options);
43
44 if (typeof options.expires === 'number') {
45 var days = options.expires, t = options.expires = new Date();
46 t.setDate(t.getDate() + days);
47 }
48
49 value = config.json ? JSON.stringify(value) : String(value);
50
51 return (document.cookie = [
52 encodeURIComponent(key), '=', config.raw ? value : encodeURIComponent(value),
53 options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
54 options.path ? '; path=' + options.path : '',
55 options.domain ? '; domain=' + options.domain : '',
56 options.secure ? '; secure' : ''
57 ].join(''));
58 }
59
60 // read
61 var decode = config.raw ? raw : decoded;
62 var cookies = document.cookie.split('; ');
63 var result = key ? undefined : {};
64 for (var i = 0, l = cookies.length; i < l; i++) {
65 var parts = cookies[i].split('=');
66 var name = decode(parts.shift());
67 var cookie = decode(parts.join('='));
68
69 if (key && key === name) {
70 result = converted(cookie);
71 break;
72 }
73
74 if (!key) {
75 result[name] = converted(cookie);
76 }
77 }
78
79 return result;
80 };
81
82 config.defaults = {};
83
84 $.removeCookie = function (key, options) {
85 if ($.cookie(key) !== undefined) {
86 $.cookie(key, '', $.extend(options, { expires: -1 }));
87 return true;
88 }
89 return false;
90 };
91
92}));