summaryrefslogtreecommitdiff
path: root/public/assets/js/cookie.js
blob: fcd3006581cfc5edfe98bb8c57c955843896d167 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// basic cookie handling
//////////////////////////////////////////////////////////////////////////////


/** create cookie
 * 
 * @param {string} name : cookie name
 * @param {*} value : cookie value
 * @param {int} expire : expiration time (in seconds)
 */
function createCookie(name, value, expire) {

    var expires;

    if (expire) {
        var date = new Date();
        date.setTime(date.getTime()+(expire * 1000));
        expires = "; expires=" + date.toGMTString();

    } else {
        expires = "";
    }

    document.cookie = name+"="+value+expires+"; path=/";
}


/** read cookie
 * 
 * @param {string} name : cookie name
 * @returns cookie value
 */
function readCookie(name) {

    var nameEQ = name + "=";
    var ca = document.cookie.split(';');

    for(var i=0 ; i < ca.length ; i++ ) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1,c.length);
        }
        if (c.indexOf(nameEQ) == 0) {
            return c.substring(nameEQ.length,c.length);
        }
    }
    return null;
}

/** remove cookie
 * == create cookie with empty string, already expired
 */
function removeCookie(name) {
    createCookie(name, "", -1);
}


/** cookie exists
 * 
 * @param {string} name 
 * @returns {boolean} true|false
 */
function cookieExists(name) {

    var nameEQ = name + "=";
    var ca = document.cookie.split(';');

    for(var i=0 ; i < ca.length ; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1, c.length);
        }
        if (c.indexOf(nameEQ) == 0) {
            return true;
        }
    }
    return false;
}