文章内容
本文主要介绍了JS使用cookie实现DIV提示框只显示一次的方法,涉及JavaScript基于cookie标记控制:
<style>
*
{
margin: 0px;
padding: 0px;
}
#cookieinfo
{
width: 600px;
background: rgba(0, 0, 0, 0.5);
font-size: 14px;
}
.info
{
padding: 32px;
}
h4
{
color: #fff;
font-size: 18px;
margin-bottom: 8px;
}
p.description
{
margin-bottom: 16px;
color: #fff;
}
.cookie-buttons
{
font-weight: 500;
text-align: right;
}
.cookie-buttons a
{
display: inline-block;
font-size: 12px;
padding: 12px 24px;
border: 1px solid #fff;
background: #fff;
color: #181818;
border-radius: 3px;
text-decoration: none;
}
.cookie-buttons a.btn1
{
margin-right: 16px;
color: #fff;
background-color: transparent;
}
</style>
<script>
function addcookie(n, v, mins, dn, path) {
if (n) {
if (!mins) mins = 365 * 24 * 60;
if (!path) path = "/";
var date = new Date();
date.setTime(date.getTime() + (mins * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
if (dn) dn = "domain=" + dn + "; ";
document.cookie = n + "=" + v + expires + "; " + dn + "path=" + path;
}
}
function getcookie(n) {
var name = n + "=";
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(name) == 0) return c.substring(name.length, c.length);
}
return "";
}
function closeclick() {
document.getElementById('cookieinfo').style.display = 'none';
addcookie('close01', 'close01', '', '', '');
}
function closebox() {
document.getElementById('cookieinfo').style.display = 'none';
}
function clickclose() {
if (getcookie('close01') == 'close01') {
document.getElementById('cookieinfo').style.display = 'none';
} else {
document.getElementById('cookieinfo').style.display = 'block';
}
}
window.onload = clickclose;
</script>
<div id="cookieinfo">
<div class="info">
<div class="text">
<h4 class="headline">
我们尊重您的隐私</h4>
<p class="description">
本网站使用第三方功能和Cookie,以确保您在访问网站时获得最佳体验。
</p>
</div>
<div class="buttons">
<div class="cookie-buttons">
<a href="javascript:;" class="btn1" onclick="closebox()">关闭</a> <a href="javascript:;"
onclick="closeclick()">已接受</a>
</div>
</div>
</div>
</div>