summaryrefslogtreecommitdiff
path: root/webcards/scripts/cookie.js
blob: d614af767c75827552a0f5d27048e172a0d08e46 (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
'use strict';

class Cookies {
    static getCookie(name){
        let cookies = document.cookie.split(";");
        for(let i in cookies) {
            let cname = cookies[i].trim().split("=")[0];
            if(cname == name){
                return cookies[i].trim().slice(name.length + 1);
            }
        }
        return "";
    }

    static setCookie(name, value, data = {}) {
        let extra = "";

        for(let key in data)
        {
            extra += "; " + key + "=" + data[key];
        }

        document.cookie = name + "=" + value + extra;
    }

    static setYearCookie(name, value) {
        var date = new Date(Date.now());
        date.setFullYear(date.getFullYear() + 1);
        Cookies.setCookie(name, value, {expires: date.toUTCString()});
    }

    static removeCookie(name) {
        var date = new Date(0);
        Cookies.setCookie(name, "", {expires: date.toUTCString()});
    }
}