mardi 28 juin 2016

js-cookie cookie value doesn't persist through refresh

I have a web page that has an advertisement on it. I'm using js-cookie to manage my cookies

Link to JS-Cookie

When a user first navigates to our website, I want javascript to check for a cookie named 'advertisement' and if it doesn't exist, to create one with the value of 'open'

    //Check for cookie. if it doesnt exist, create it
    var cookie = Cookies.get("Advertisement");
    alert(cookie);
    if (cookie == "undefined" || cookie == null || cookie == "" || cookie == "null") {
        CreateCookieAdvertisement();
    } 

function CreateCookieAdvertisement() {
    alert("Creating cookie function");
    Cookies.set("Advertisement", "open");
}

The advertisement has a anchor tag that has a javascript function to set the value of 'Advertisement' to 'closed'

<a class="close" id="closeAdvertistement" onclick="CloseCookieAdvertisement()">&nbsp;</a>

function CloseCookieAdvertisement() {
    Cookies.remove('Advertisement');
    Cookies.set('Advertistement', 'closed');
}

Here is the problem. When I first load the site, it determines the value is undefined and creates the cookie with a value of 'open' 1cookieOpen

Then, after I close the advertisement, it sets the cookie to 'closed'

1cookieclosed

But after doing a refresh, for some reason it creates an additional cookie named 'advertisement' with a value of 'open' while the original 'advertisement' still has a value of 'closed'

2Cookies

Here is a fiddle to my whole code minus the html for the advertisement. I don't know if jsfiddle allows you to create cookies or not so I'm not sure if this will work

https://jsfiddle.net/x43nbbmk/

Aucun commentaire:

Enregistrer un commentaire