├── license ├── src └── jecookie.js └── README.md /license: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011 Alejandro El Infromático 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /src/jecookie.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Cookie utils 3 | * 4 | * @author Alejandro El Informático 5 | * 6 | * @version 1.1.1 7 | * 8 | * @created 20111204 9 | * 10 | * @modified 20120126 11 | * 12 | **/ 13 | 14 | /** 15 | * Main object 16 | * 17 | * @param name cookie name 18 | * 19 | * @param data cookie data 20 | * 21 | * @param opts options 22 | * 23 | * @param opts.secure set secure 24 | * 25 | * @param opts.domain domain 26 | * 27 | * @param opts.path path 28 | * 29 | * @param opts.expires expiration, use 0 to session cookie 30 | * 31 | * */ 32 | var jecookie = function (name, data, opts) 33 | { 34 | this.name = name || undefined; //cookie name 35 | this.data = data || {}; //object data 36 | this.opts = opts || {}; 37 | this.dt = document; 38 | }; 39 | 40 | /** 41 | * Define prototype 42 | * 43 | * */ 44 | jecookie.prototype = 45 | { 46 | /** 47 | * Convert to JSON 48 | * 49 | * @param d 50 | * 51 | * @return object or string 52 | * 53 | * */ 54 | to_json : function(d) 55 | { 56 | d = unescape(d); 57 | if(d.match(/^({(.*)})|(\[(.*)\])$/)) //is an object or array 58 | { 59 | if(typeof JSON === 'object') //native support 60 | { 61 | return JSON.parse(d); 62 | } 63 | else if(typeof jQuery === 'function') //jquery support 64 | { 65 | return jQuery.parseJSON(d); 66 | } 67 | } 68 | return d; //fallback 69 | }, 70 | 71 | /** 72 | * Convert object to string 73 | * 74 | * @param d data to convert 75 | * 76 | * @return string 77 | * 78 | * */ 79 | to_string : function(d) 80 | { 81 | if(typeof d === 'object') //is an object 82 | { 83 | if(typeof JSON === 'object') //native support 84 | { 85 | return escape(JSON.stringify(d)); 86 | } 87 | } 88 | return d; //fallback 89 | }, 90 | 91 | /** 92 | * Save cookie 93 | * 94 | * @param e date when cookie expires 95 | * 96 | * */ 97 | save : function(e) 98 | { 99 | var _this = this, 100 | opts = _this.opts, 101 | json = _this.to_string(_this.data), //convert data 102 | path = opts.path || '/', //default path 103 | domain = opts.domain || false, //default domain 104 | secure = opts.secure || false, //by default is not set secure 105 | expires = (typeof e === 'undefined') ? new Date(2020, 01, 01) : e, //expiration date 106 | cookie = _this.name +'=' + json; //create the cookie string 107 | (typeof opts.expires !== 'undefined') && (expires = opts.expires); //overwrite expiration 108 | domain && (cookie += ';domain=' + domain); 109 | path && (cookie += ';path=' + path); //set path 110 | (expires !== 0) && (cookie += ';expires=' + expires); //set expiration 111 | secure && (cookie += ';secure'); //set secure 112 | _this.dt.cookie = cookie; //write cookie 113 | }, 114 | 115 | /** 116 | * Load cookie content 117 | * 118 | * @return this.data if cookie exist 119 | * 120 | * @return undefined if cookie does not exist 121 | * 122 | * */ 123 | load : function() 124 | { 125 | var _this = this, 126 | test = _this.dt.cookie.split(';'), //set the split 127 | regex = new RegExp('(?:; )?' + _this.name + '=([^;]*);?'); //create the regex 128 | if(regex.test(_this.dt.cookie)) //can load the cookie? 129 | { 130 | _this.data = _this.to_json(RegExp['$1']); 131 | return _this.data; 132 | } 133 | return undefined; 134 | }, 135 | 136 | /** 137 | * Destroy cookie 138 | * 139 | * */ 140 | destroy : function() 141 | { 142 | this.save(new Date(0)); 143 | } 144 | }; 145 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | jecookie 2 | ============================================== 3 | Javascript easy cookie manipulation. 4 | 5 | Requirements 6 | --------------------------------------------- 7 | The below requirements are only if you want to save and load `json` objects to cookie, if not you can save raw data. 8 | 9 | * `JSON` to save `json` objects to cookie using `stringify` method. 10 | * Please consider browser native support or only copy the `stringify` method to your utilities. 11 | * `jQuery` to convert loaded content to `json` object. 12 | 13 | Usage 14 | ---------------------------------------------- 15 | ###Create a cookie 16 | var cookie = new jecookie('cookie_name'); 17 | var cookie = new jecookie('cookie_name', {name : 'value'}, {secure :true, domain : '.example.org'}); 18 | 19 | Params: 20 | 21 | * `name`: cookie name 22 | * `data`: data 23 | * `opts`: options 24 | * `opts.secure` set secure 25 | * `opts.domain` domain 26 | * `opts.path` path 27 | * `opts.expires` expiration, use 0 to session cookie 28 | 29 | ###Load a cookie 30 | cookie.load(); 31 | 32 | ###Change data 33 | If `JSON` support: 34 | 35 | cookie.data = {name : value, content : value}; 36 | 37 | Otherwise: 38 | 39 | cookie.data = value; 40 | 41 | ###Change options 42 | cookie.opts.secure = true; 43 | cookie.opts.domain = '.example.org' 44 | 45 | ###Save data 46 | cookie.save(); //default expiration 47 | cookie.save(0); //session cookie 48 | 49 | ###Destroy a cookie 50 | cookie.destroy(); 51 | 52 | Example 53 | ---------------------------------------------- 54 | //... 55 | var cookie = new jecookie('settings'); 56 | if(cookie.load()) //cookie exists 57 | { 58 | my_element.height = cookie.data.height; 59 | my_element.width = cookie.data.width; 60 | } 61 | else //cookie does not exists 62 | { 63 | cookie.data = 64 | { 65 | height : 100, 66 | width : 100 67 | }; 68 | cookie.save(); 69 | } 70 | //... 71 | 72 | Support 73 | ---------------------------------------------- 74 | * IE 6+ 75 | * FF 3+ 76 | * Chrome 10+ 77 | * Opera 10+ 78 | 79 | Version history 80 | ---------------------------------------------- 81 | * **1.0** 82 | * Basic base code 83 | * **1.1** 84 | * Third parameter as an options object in constructor 85 | * Removed path param in save method 86 | * Domain, path and session cookie support 87 | * Control for array in loaded data 88 | * **1.1.1** 89 | * Change to MIT license 90 | 91 | Author 92 | ---------------------------------------------- 93 | Alejandro El Informático 94 | 95 | License 96 | ---------------------------------------------- 97 | The MIT license 98 | 99 | Copyright (c) 2011 Alejandro El Infromático 100 | 101 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 102 | 103 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 104 | 105 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 106 | --------------------------------------------------------------------------------