├── README.markdown └── jquery.session.js /README.markdown: -------------------------------------------------------------------------------- 1 | #JQuery Session Plugin 2 | Provides a simple interface into window.sessionStorage while providing limited support for cross protocol requests. 3 | 4 | ##Usage 5 | ``` 6 | $.session.set('some key', 'a value'); 7 | 8 | $.session.get('some key'); 9 | > "a value" 10 | 11 | $.session.clear(); 12 | 13 | $.session.get('some key'); 14 | > undefined 15 | 16 | $.session.set('some key', 'a value').get('some key'); 17 | > "a value" 18 | 19 | $.session.remove('some key'); 20 | 21 | $.session.get('some key'); 22 | > undefined 23 | ``` 24 | -------------------------------------------------------------------------------- /jquery.session.js: -------------------------------------------------------------------------------- 1 | (function($){ 2 | 3 | $.session = { 4 | 5 | _id: null, 6 | 7 | _cookieCache: undefined, 8 | 9 | _init: function() 10 | { 11 | if (!window.name) { 12 | window.name = Math.random(); 13 | } 14 | this._id = window.name; 15 | this._initCache(); 16 | 17 | // See if we've changed protcols 18 | 19 | var matches = (new RegExp(this._generatePrefix() + "=([^;]+);")).exec(document.cookie); 20 | if (matches && document.location.protocol !== matches[1]) { 21 | this._clearSession(); 22 | for (var key in this._cookieCache) { 23 | try { 24 | window.sessionStorage.setItem(key, this._cookieCache[key]); 25 | } catch (e) {}; 26 | } 27 | } 28 | 29 | document.cookie = this._generatePrefix() + "=" + document.location.protocol + ';path=/;expires=' + (new Date((new Date).getTime() + 120000)).toUTCString(); 30 | 31 | }, 32 | 33 | _generatePrefix: function() 34 | { 35 | return '__session:' + this._id + ':'; 36 | }, 37 | 38 | _initCache: function() 39 | { 40 | var cookies = document.cookie.split(';'); 41 | this._cookieCache = {}; 42 | for (var i in cookies) { 43 | var kv = cookies[i].split('='); 44 | if ((new RegExp(this._generatePrefix() + '.+')).test(kv[0]) && kv[1]) { 45 | this._cookieCache[kv[0].split(':', 3)[2]] = kv[1]; 46 | } 47 | } 48 | }, 49 | 50 | _setFallback: function(key, value, onceOnly) 51 | { 52 | var cookie = this._generatePrefix() + key + "=" + value + "; path=/"; 53 | if (onceOnly) { 54 | cookie += "; expires=" + (new Date(Date.now() + 120000)).toUTCString(); 55 | } 56 | document.cookie = cookie; 57 | this._cookieCache[key] = value; 58 | return this; 59 | }, 60 | 61 | _getFallback: function(key) 62 | { 63 | if (!this._cookieCache) { 64 | this._initCache(); 65 | } 66 | return this._cookieCache[key]; 67 | }, 68 | 69 | _clearFallback: function() 70 | { 71 | for (var i in this._cookieCache) { 72 | document.cookie = this._generatePrefix() + i + '=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;'; 73 | } 74 | this._cookieCache = {}; 75 | }, 76 | 77 | _deleteFallback: function(key) 78 | { 79 | document.cookie = this._generatePrefix() + key + '=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;'; 80 | delete this._cookieCache[key]; 81 | }, 82 | 83 | get: function(key) 84 | { 85 | return window.sessionStorage.getItem(key) || this._getFallback(key); 86 | }, 87 | 88 | set: function(key, value, onceOnly) 89 | { 90 | try { 91 | window.sessionStorage.setItem(key, value); 92 | } catch (e) { 93 | // doing it after catch defeats purpose of fallback 94 | this._setFallback(key, value, onceOnly || false); 95 | } 96 | return this; 97 | }, 98 | 99 | 'delete': function(key){ 100 | return this.remove(key); 101 | }, 102 | 103 | remove: function(key) 104 | { 105 | try { 106 | window.sessionStorage.removeItem(key); 107 | } catch (e) {}; //here it's correct because get() returns value from any source 108 | this._deleteFallback(key); 109 | return this; 110 | }, 111 | 112 | _clearSession: function() 113 | { 114 | try { 115 | window.sessionStorage.clear(); 116 | } catch (e) { 117 | for (var i in window.sessionStorage) { 118 | window.sessionStorage.removeItem(i); 119 | } 120 | } 121 | }, 122 | 123 | clear: function() 124 | { 125 | this._clearSession(); 126 | this._clearFallback(); 127 | return this; 128 | } 129 | 130 | }; 131 | 132 | $.session._init(); 133 | 134 | })(jQuery); 135 | --------------------------------------------------------------------------------