source: documentation/trunk/packages/dokuwiki-2011-05-25a/lib/scripts/cookie.js@ 25027

Last change on this file since 25027 was 25027, checked in by jmt12, 12 years ago

Adding the packages directory, and within it a configured version of dokuwiki all ready to run

File size: 3.1 KB
Line 
1/**
2 * Handles the cookie used by several JavaScript functions
3 *
4 * Only a single cookie is written and read. You may only save
5 * sime name-value pairs - no complex types!
6 *
7 * You should only use the getValue and setValue methods
8 *
9 * @author Andreas Gohr <[email protected]>
10 */
11DokuCookie = {
12 data: Array(),
13 name: 'DOKU_PREFS',
14
15 /**
16 * Save a value to the cookie
17 *
18 * @author Andreas Gohr <[email protected]>
19 */
20 setValue: function(key,val){
21 DokuCookie.init();
22 DokuCookie.data[key] = val;
23
24 // prepare expire date
25 var now = new Date();
26 DokuCookie.fixDate(now);
27 now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000); //expire in a year
28
29 //save the whole data array
30 var text = '';
31 for(var key in DokuCookie.data){
32 if (!DokuCookie.data.hasOwnProperty(key)) continue;
33 text += '#'+escape(key)+'#'+DokuCookie.data[key];
34 }
35 DokuCookie.setCookie(DokuCookie.name,text.substr(1),now,DOKU_BASE);
36 },
37
38 /**
39 * Get a Value from the Cookie
40 *
41 * @author Andreas Gohr <[email protected]>
42 */
43 getValue: function(key){
44 DokuCookie.init();
45 return DokuCookie.data[key];
46 },
47
48 /**
49 * Loads the current set cookie
50 *
51 * @author Andreas Gohr <[email protected]>
52 */
53 init: function(){
54 if(DokuCookie.data.length) return;
55 var text = DokuCookie.getCookie(DokuCookie.name);
56 if(text){
57 var parts = text.split('#');
58 for(var i=0; i<parts.length; i+=2){
59 DokuCookie.data[unescape(parts[i])] = unescape(parts[i+1]);
60 }
61 }
62 },
63
64 /**
65 * This sets a cookie by JavaScript
66 *
67 * @link http://www.webreference.com/js/column8/functions.html
68 */
69 setCookie: function(name, value, expires, path, domain, secure) {
70 var curCookie = name + "=" + escape(value) +
71 ((expires) ? "; expires=" + expires.toGMTString() : "") +
72 ((path) ? "; path=" + path : "") +
73 ((domain) ? "; domain=" + domain : "") +
74 ((secure) ? "; secure" : "");
75 document.cookie = curCookie;
76 },
77
78 /**
79 * This reads a cookie by JavaScript
80 *
81 * @link http://www.webreference.com/js/column8/functions.html
82 */
83 getCookie: function(name) {
84 var dc = document.cookie;
85 var prefix = name + "=";
86 var begin = dc.indexOf("; " + prefix);
87 if (begin == -1) {
88 begin = dc.indexOf(prefix);
89 if (begin !== 0){ return null; }
90 } else {
91 begin += 2;
92 }
93 var end = document.cookie.indexOf(";", begin);
94 if (end == -1){
95 end = dc.length;
96 }
97 return unescape(dc.substring(begin + prefix.length, end));
98 },
99
100 /**
101 * This is needed for the cookie functions
102 *
103 * @link http://www.webreference.com/js/column8/functions.html
104 */
105 fixDate: function(date) {
106 var base = new Date(0);
107 var skew = base.getTime();
108 if (skew > 0){
109 date.setTime(date.getTime() - skew);
110 }
111 }
112};
Note: See TracBrowser for help on using the repository browser.