|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +(function ($,undefined) { |
| 4 | + function decode (s) { |
| 5 | + try { |
| 6 | + return decodeURIComponent(s); |
| 7 | + } |
| 8 | + catch (e) { |
| 9 | + try { |
| 10 | + return unescape(s); |
| 11 | + } |
| 12 | + catch (e2) { |
| 13 | + return s; |
| 14 | + } |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + function get () { |
| 19 | + var cookies = {}; |
| 20 | + if (document.cookie) { |
| 21 | + var values = document.cookie.split(/; */g); |
| 22 | + for (var i = 0; i < values.length; ++ i) { |
| 23 | + var value = values[i]; |
| 24 | + var pos = value.search("="); |
| 25 | + var key, value; |
| 26 | + |
| 27 | + if (pos < 0) { |
| 28 | + key = decode(value); |
| 29 | + value = undefined; |
| 30 | + } |
| 31 | + else { |
| 32 | + key = decode(value.slice(0,pos)); |
| 33 | + value = decode(value.slice(pos+1)); |
| 34 | + } |
| 35 | + |
| 36 | + cookies[key] = value; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + return cookies; |
| 41 | + } |
| 42 | + |
| 43 | + function set (name, value, expires, path, domain, secure) { |
| 44 | + switch (arguments.length) { |
| 45 | + case 1: |
| 46 | + for (var key in name) { |
| 47 | + set(key, name[key]); |
| 48 | + } |
| 49 | + return; |
| 50 | + |
| 51 | + case 2: |
| 52 | + if (typeof(value) === "object") { |
| 53 | + expires = value.expires; |
| 54 | + path = value.path; |
| 55 | + domain = value.domain; |
| 56 | + secure = value.secure; |
| 57 | + value = value.value; |
| 58 | + } |
| 59 | + |
| 60 | + case 3: |
| 61 | + if (typeof(expires) === "object" && !(expires instanceof Date)) { |
| 62 | + path = expires.path; |
| 63 | + domain = expires.domain; |
| 64 | + secure = expires.secure; |
| 65 | + expires = expires.expires; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if (value === null || value === undefined) { |
| 70 | + expires = -1; |
| 71 | + } |
| 72 | + |
| 73 | + var buf = [encodeURIComponent(name)+'='+encodeURIComponent(value)]; |
| 74 | + switch (typeof(expires)) { |
| 75 | + case "string": |
| 76 | + expires = new Date(expires); |
| 77 | + |
| 78 | + case "object": |
| 79 | + buf.push("expires="+expires.toUTCString()); |
| 80 | + break; |
| 81 | + |
| 82 | + case "boolean": |
| 83 | + if (expires) { |
| 84 | + break; |
| 85 | + } |
| 86 | + expires = 365*2000; |
| 87 | + |
| 88 | + case "number": |
| 89 | + var date = new Date(); |
| 90 | + date.setDate(date.getDate()+expires); |
| 91 | + buf.push("expires="+date.toUTCString()); |
| 92 | + break; |
| 93 | + } |
| 94 | + |
| 95 | + if (path === true) { |
| 96 | + buf.push("path="+document.location.pathname); |
| 97 | + } |
| 98 | + else if (path) { |
| 99 | + buf.push("path="+path.replace(/[;\s]/g,encodeURIComponent)); |
| 100 | + } |
| 101 | + |
| 102 | + if (domain === true) { |
| 103 | + buf.push("domain="+document.location.host); |
| 104 | + } |
| 105 | + else if (domain) { |
| 106 | + buf.push("domain="+domain.replace(/[;\s]/g,encodeURIComponent)); |
| 107 | + } |
| 108 | + |
| 109 | + if (secure) { |
| 110 | + buf.push("secure"); |
| 111 | + } |
| 112 | + |
| 113 | + document.cookie = buf.join("; "); |
| 114 | + } |
| 115 | + |
| 116 | + $.cookie = function (name) { |
| 117 | + switch (arguments.length) { |
| 118 | + case 0: |
| 119 | + return get(); |
| 120 | + case 1: |
| 121 | + if (typeof(name) !== "object") { |
| 122 | + var cookies = get(); |
| 123 | + if (name === undefined) { |
| 124 | + return cookies; |
| 125 | + } |
| 126 | + else if (Object.prototype.hasOwnProperty.call(cookies,name)) { |
| 127 | + return cookies[name]; |
| 128 | + } |
| 129 | + return null; |
| 130 | + } |
| 131 | + case 2: |
| 132 | + case 3: |
| 133 | + case 4: |
| 134 | + case 5: |
| 135 | + case 6: |
| 136 | + set.apply(this,arguments); |
| 137 | + return this; |
| 138 | + |
| 139 | + default: |
| 140 | + throw new Error("Illegal number of arguments"); |
| 141 | + } |
| 142 | + }; |
| 143 | + |
| 144 | + $.removeCookie = function (name) { |
| 145 | + var cookies = get(); |
| 146 | + if (Object.prototype.hasOwnProperty.call(cookies,name)) { |
| 147 | + var args = Array.prototype.slice.call(arguments,1); |
| 148 | + args.unshift(name,null,-1); |
| 149 | + set.apply(this,args); |
| 150 | + return true; |
| 151 | + } |
| 152 | + return false; |
| 153 | + }; |
| 154 | +})(jQuery); |
0 commit comments