Set/Get/Erase Cookies

Set/Get/Erase Cookies

These are three functions setCookie , getCookie, eraseCookie to deal with cookies on WP site like you want to hide a popup after user saw it or use closed popup and that popup should not appear for a number of days like

function setCookie(name,value,days) {
var expires = "";
if (days) {
    var 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) {
    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;
}
function eraseCookie(name) {   
    document.cookie = name+'=; Max-Age=-99999999;';  
}

You can redirect to internal page if cookie is already set in setCookie

window.location.href = object_name.legal_drinking_age_url;

set cookie img

We can also use this to close the popup and keep it closed for 30 days based on cookie.

session img1

Leave a Reply

Your email address will not be published. Required fields are marked *