top of page

Set/Get/Erase Cookies

  • Writer: سُلَيْمَان بْن دَاوُوْد
    سُلَيْمَان بْن دَاوُوْد
  • May 21, 2020
  • 1 min read

Updated: Jun 6, 2020

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;

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




Recent Posts

See All
Google ReCAPTCHA with AJAX

This is how you can pass on AJAX .js var userdata = { 'action' : 'signin_member', 'username' : jQuery('#user_name').val(), 'password' :...

 
 
 
Self Invoking Function jQuery

The function starts with (function ($) { and you can use your own code inside it (function ($) { jQuery(document).ready(function () {...

 
 
 
Smooth Scroll In JS and jQuery

jQuery(document).ready(function () { jQuery('.processreachfind').click(function(e){ e.preventDefault(); jQuery('html, body').animate({...

 
 
 

Comments


© 2020 by syednazrulhassan

bottom of page