简体   繁体   中英

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 of the page and comes back, the popup won't appear until he ends the session (closes the browser). Thanks!

<script type="text/javascript">
var link;
var element;
t = setTimeout(openPopUp, 3000);

function openPopUp() {
element = document.getElementById("background");
element.style.display = "block";
element = document.getElementById("popup");
element.style.display = "block";
}

function closePopUp() {
element = document.getElementById("popup");
element.style.display = "none";
element = document.getElementById("background");
element.style.display = "none";
}
</script>

Edit: From comments:

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

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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM