I have what I would think is a pretty simple code issue, but nonetheless. Basically, everything works as it should except all I need is to remove a class from the document body if a cookie is present. I have a cookie policy popup overlay that appears on first visit. When that is accepted a cookie is set. However, if the cookie is set I no longer want the side menu to be expanded, which is merely a class in the body called 'show-menu'.
I understand that all you need to do to remove a class from the body is:
document.body.classList.remove('show-menu');
However, the only place in my cookie.js file that references checking if there is a cookie, and whether to display the overlay again or not is here:
if (this.options.cookieAlert)
{
if (getCookie(this.options.cookieName) !== '')
{
this.destroy();
return false;
}
if (this.options.cookieAcceptButtonId !== null)
{
document.getElementById(this.options.cookieAcceptButtonId).addEventListener('click', function(e){
e.preventDefault();
setCookie(that.options.cookieName, 'yes', that.options.cookieExpireDay);
that.destroy();
});
}
}
I tried adding the classList.remove('show-menu') line above 'this.destroy();' to see if it would at least remove the 'show-menu' class from the body before it canceled the function.
And here is the 'getCookie' var code. I wasn't able to add the classList.remove line to this either and get it to work.
var getCookie = function(cname) {
var name = cname + "=";
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);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}