Subversion-Projekte lars-tiefland.faltradxxs.de

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
2 lars 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
            this._setFallback(key, value, onceOnly || false);
94
            return this;
95
        },
96
 
97
        'delete': function(key){
98
            return this.remove(key);
99
        },
100
 
101
        remove: function(key)
102
        {
103
            try {
104
            window.sessionStorage.removeItem(key);
105
            } catch (e) {};
106
            this._deleteFallback(key);
107
            return this;
108
        },
109
 
110
        _clearSession: function()
111
        {
112
          try {
113
                window.sessionStorage.clear();
114
            } catch (e) {
115
                for (var i in window.sessionStorage) {
116
                    window.sessionStorage.removeItem(i);
117
                }
118
            }
119
        },
120
 
121
        clear: function()
122
        {
123
            this._clearSession();
124
            this._clearFallback();
125
            return this;
126
        }
127
 
128
    };
129
 
130
    $.session._init();
131
 
132
})(jQuery);