Skip to content Skip to sidebar Skip to footer

Popup Cookie Once Per Browser Session

I have a very simple popup, I would like to set a cookie, so when the visitor visits the page / category for the first time he will be able to see the popup and if he navigates out

Solution 1:

Something in the like (untested code):

var link;

// ....


if(getCookie("was_here_before")=="") {
    setTimeout(openPopUp, 3000);
    createCookie("was_here_before", "yes", 1);
}

// ....



// these are from SO answer in first comment by Sverri M. Olsen
// [ http://stackoverflow.com/questions/4825683/how-do-i-create-and-read-a-value-from-cookie ]



function createCookie(name, value, days) {
    var expires;
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    }
    else {
        expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) {
                c_end = document.cookie.length;
            }
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}


// ....


function openPopUp(url) {
    var element;
    link = url; // don't know what this is for
    element = document.getElementById("background");
    element.style.display = "block";
    element = document.getElementById("popup");
    element.style.display = "block";
}

function closePopUp() {
    var element;
    element = document.getElementById("popup");
    element.style.display = "none";
    element = document.getElementById("background");
    element.style.display = "none";
}

Post a Comment for "Popup Cookie Once Per Browser Session"