我有一个非常简单的弹出窗口,我想设置一个cookie,所以当访问者第一次访问页面/类别时,他将能够看到弹出窗口,如果他导航出页面并返回,弹出窗口不会出现,直到他结束会话(关闭浏览器)。谢谢!
<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=/";
}
答案 0 :(得分:1)
类似的东西(未经测试的代码):
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";
}