-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
64 lines (52 loc) · 1.23 KB
/
app.js
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
const btn = document.querySelector("#btn-ab");
const getCookie = (cookieName) => {
const name = cookieName + "=";
const cookieParts = document.cookie.split(";");
for (let i = 0; i < cookieParts.length; i++) {
let part = cookieParts[i];
while (part.charAt(0) == " ") {
part = part.substring(1);
}
if (part.indexOf(name) == 0) {
return part.substring(name.length, part.length);
}
}
return "";
};
const checkCookie = () => {
const netlifyCookie = getCookie("nf_ab");
if (netlifyCookie) {
btn.innerHTML = "Opt-out";
}
};
window.addEventListener("load", () => {
checkCookie();
});
const buttonHandler = (button, callback) => {
if (!button) return;
button.addEventListener(
"click",
(event) => {
event.preventDefault();
callback();
},
false,
);
};
buttonHandler(btn, () => {
switch (btn.innerHTML) {
case "Opt-in":
btn.innerHTML = "Opt-out";
const now = new Date();
const expires = now.getTime() + 1000 * 3600 * 24 * 365;
now.setTime(expires);
document.cookie = `nf_ab=test; expires=${now.toUTCString()}`;
location.reload();
break;
case "Opt-out":
btn.innerHTML = "Opt-in";
document.cookie = "nf_ab=;expires=Thu, 01 Jan 1970 00:00:01 GMT;";
location.reload();
break;
}
});