Skip to content

Instantly share code, notes, and snippets.

@ihorduchenko
Created December 13, 2021 22:16
Show Gist options
  • Save ihorduchenko/1c11e7d28a10ca5af8731c39be5b8a72 to your computer and use it in GitHub Desktop.
Save ihorduchenko/1c11e7d28a10ca5af8731c39be5b8a72 to your computer and use it in GitHub Desktop.
Get and set cookies on first user visit
function setCookie(name, value, days) {
var expires = '';
if (days) {
let date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = '; expires=' + date.toUTCString();
}
document.cookie = name + '=' + (value || '') + expires + '; path=/';
}
function getCookie(name) {
let nameEQ = name + '=';
let ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (' ' == c.charAt(0)) {
c = c.substring(1, c.length);
if (0 == c.indexOf(nameEQ)) {
return c.substring(nameEQ.length, c.length);
}
}
}
return null;
}
(function() {
let firstVisitCookie = getCookie('firstVisitCookie');
let firstVisitModal = $('#firstViisitModal');
if (! firstVisitCookie && firstVisitModal.length) {
let subscribeForm = firstVisitModal.find('form');
firstVisitModal.modal('show');
subscribeForm.on('submit', function() {
setCookie('firstVisitCookie', '1', 30);
});
firstVisitModal.on('hide.bs.modal', function() {
setCookie('firstVisitCookie', '1', 30);
});
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment