1/** 2 * js_cookie.js 3 * 4 * usage: 5 * var cookie = new jsCookie('KEY', { 6 * path : '/', 7 * secure : true 8 * }); 9 * 10 * cookie.write('value', 1); 11 * //cookie.write('value', ''); # ex.End of session 12 * 13 * var value = cookie.read(); 14 * 15 * cookie.remove(); 16 */ 17'use strict'; 18 19var jsCookie = function(key, options) { 20 this.vars = { 21 key : null, 22 options : { 23 path : location.pathname.replace(/\/[^\/]*$/, '/'), 24 domain : location.hostname, 25 expires : '', 26 secure : true, 27 httponly : '' 28 } 29 }; 30 this.init(key, options); 31}; 32 33jsCookie.prototype.init = function(key, options) { 34 // KEYã¨ãã¦å©ç¨ã§ããæåã¯ãè±æ°å㨠_ - ã®ã¿ 35 if (key == '' || ! key.match(/^[0-9a-zA-Z|_\-]+$/)) { return; } 36 this.vars.key = key; 37 // optionããã©ã«ãã®è¨å® 38 this.vars.options = this.getOptions__(options); 39}; 40// 弿°ãããã°ãããåªå ãã¤ã¤å®å ¨ãªoptionsãè¿ã 41jsCookie.prototype.getOptions__ = function(options) { 42 if (! options) { options = {}; } 43 var opt = {}; 44 for (var key in this.vars.options) { 45 if (options.hasOwnProperty(key)) { 46 opt[key] = options[key]; 47 } 48 else if (this.vars.options.hasOwnProperty(key)) { 49 opt[key] = this.vars.options[key]; 50 } 51 } 52 // expiresãæ°åã®å ´å㯠næ¥å¾ã¨ãã 53 if (typeof(opt.expires) === 'number') { 54 var day = opt.expires; 55 var expires_day = new Date(); 56 expires_day.setDate(expires_day.getDate() + day); 57 opt.expires = expires_day; 58 } 59 60 return opt; 61}; 62 63jsCookie.prototype.read = function() { 64 if (! this.vars.key) { return null; } 65 var value = this.parse__(this.vars.key); 66 return value; 67}; 68jsCookie.prototype.parse__ = function(key) { 69 var cookies = document.cookie ? document.cookie.split('; ') : []; 70 for (var i in cookies) { 71 var parts = cookies[i].split('='); 72 if (parts[0] == encodeURIComponent(key)) { 73 var encoded_str = parts[1]; 74 return decodeURIComponent(encoded_str); 75 } 76 } 77 return null; 78}; 79 80jsCookie.prototype.write = function(value, expires_day, options) { 81 if (! this.vars.key) { return null; } 82 if (!options) { options = { expires : expires_day }; } 83 else { options.expires = expires_day; } 84 var opt = this.getOptions__(options); 85 86 var cookie_text = [ 87 encodeURIComponent(this.vars.key), '=', encodeURIComponent(value), 88 opt.expires ? '; Expires=' + opt.expires.toUTCString() : '', 89 opt.path ? '; path=' + opt.path : '', 90 opt.domain ? '; Domain=' + opt.domain : '', 91 opt.secure ? '; Secure' : '', 92 opt.httponly ? '; HttpOnly' : '' 93 ].join(''); 94 console.log(cookie_text); 95 document.cookie = cookie_text; 96}; 97 98jsCookie.prototype.remove = function(options) { 99 this.write('', -1, options); 100};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.