如何使用 cookie 隐藏 div 24 小时
How to hide a div for 24 hours with a cookie
我需要将此 div
隐藏 24 小时。 put
没用。
我想要一个简单的 JavaScript 代码,它可以让我在点击预定时间后隐藏某个 div
元素。
为了提供更多信息,我有一个在加载主页时出现的建议框。我想要的是当 div
关闭按钮被点击时,它设置一个 cookie 来保持框 div
关闭 24 小时(1 天)。
简单地说,当div
关闭按钮被按下时,框div隐藏24小时。
注意:我有一个 javascript 代码允许关闭按钮关闭框,但它会在每次刷新时重新加载。
Link to the project's file (小编点评:从不受信任的网站下载文件不安全,还是等作者用代码编辑问题吧。)
我注意到 #splashscreen
默认设置为可见。您可以默认隐藏它,仅当 cookie 不存在 时才显示它。或者更简单地添加 else statement
并在存在 cookie 时将其隐藏。请更改您的 JS 代码的第一个 if statement
,如下所示。
// If the 'hide cookie is not set we show the message
if (!readCookie('hide')) {
$('#splashscreen').show();
} else {
$('#splashscreen').hide();
}
谢谢大家当前的代码是
$(document).ready(function() {
// If the 'hide cookie is not set we show the message
if (!readCookie('hide')) {
$('#applink').show();
}else {
$('#applink').hide();
}
// Add the event that closes the popup and sets the cookie that tells us to
// not show it again until one day has passed.
$('#playstorclose').click(function() {
$('#applink').hide();
createCookie('hide', true, 1)
return false;
});
});
// ---
// And some generic cookie logic
// ---
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
<div class="playstorapp" id="applink" style="text-align: center;margin: auto">
<a id="playstorclose" href="JavaScript:void(0)">X</a>
<a class="applink" href="https://play.google.com/store/apps/details?id=com.aradev.net&rdid=com.aradev.net" target="_blank">
Download
</a>
</div>
我需要将此 div
隐藏 24 小时。 put
没用。
我想要一个简单的 JavaScript 代码,它可以让我在点击预定时间后隐藏某个 div
元素。
为了提供更多信息,我有一个在加载主页时出现的建议框。我想要的是当 div
关闭按钮被点击时,它设置一个 cookie 来保持框 div
关闭 24 小时(1 天)。
简单地说,当div
关闭按钮被按下时,框div隐藏24小时。
注意:我有一个 javascript 代码允许关闭按钮关闭框,但它会在每次刷新时重新加载。
Link to the project's file (小编点评:从不受信任的网站下载文件不安全,还是等作者用代码编辑问题吧。)
我注意到 #splashscreen
默认设置为可见。您可以默认隐藏它,仅当 cookie 不存在 时才显示它。或者更简单地添加 else statement
并在存在 cookie 时将其隐藏。请更改您的 JS 代码的第一个 if statement
,如下所示。
// If the 'hide cookie is not set we show the message
if (!readCookie('hide')) {
$('#splashscreen').show();
} else {
$('#splashscreen').hide();
}
谢谢大家当前的代码是
$(document).ready(function() {
// If the 'hide cookie is not set we show the message
if (!readCookie('hide')) {
$('#applink').show();
}else {
$('#applink').hide();
}
// Add the event that closes the popup and sets the cookie that tells us to
// not show it again until one day has passed.
$('#playstorclose').click(function() {
$('#applink').hide();
createCookie('hide', true, 1)
return false;
});
});
// ---
// And some generic cookie logic
// ---
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
<div class="playstorapp" id="applink" style="text-align: center;margin: auto">
<a id="playstorclose" href="JavaScript:void(0)">X</a>
<a class="applink" href="https://play.google.com/store/apps/details?id=com.aradev.net&rdid=com.aradev.net" target="_blank">
Download
</a>
</div>