gpt4 book ai didi

javascript - 每个浏览器 session 弹出一次 cookie

转载 作者:行者123 更新时间:2023-12-03 11:47:29 25 4
gpt4 key购买 nike

我有一个非常简单的弹出窗口,我想设置一个 cookie,这样当访问者第一次访问页面/类别时,他将能够看到弹出窗口,并且如果他导航出页面并返回,直到他结束 session (关闭浏览器)后才会出现弹出窗口。谢谢!

<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>

编辑:来自评论:

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

最佳答案

类似的东西(未经测试的代码):

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

关于javascript - 每个浏览器 session 弹出一次 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25986726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com