javascript - 添加 cookie 只显示一次弹出窗口
问题描述
我有一个作业,我需要帮助来修复它 我有一个弹出代码 HTML
/*
You'll probably want to drop a cookie so this doesn't pop up everytime. I'd recommend this plugin:
https://github.com/js-cookie/js-cookie
*/
overAge = function () {
$('#age-verify').addClass('hidden');
}
underAge = function () {
$('#age-verify').addClass('under');
}
goBack = function () {
window.history.back();
}
#age-verify {
position: fixed;
z-index: 9997;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.6);
transition: 500ms;
}
#age-verify .window {
position: absolute;
top: 50%;
left: 50%;
width: 400px;
height: 250px;
overflow: hidden;
padding: 40px;
margin-left: -200px;
margin-top: -125px;
background-color: #fff;
border: 6px solid #ED6A5A;
box-sizing: border-box;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
transition: 500ms;
z-index: 9998;
}
#age-verify .window span {
display: block;
text-align: center;
margin-bottom: 10px;
font-family: "Source Sans Pro", sans-serif;
}
#age-verify .window span.title {
color: #ED6A5A;
font-size: 24px;
}
#age-verify .window button {
border: 0;
margin: 0;
padding: 0;
width: 48%;
height: 60px;
color: #FFF;
font-size: 18px;
background-color: #ED6A5A;
margin-top: 20px;
font-family: "Source Sans Pro", sans-serif;
-webkit-transform: scale(1);
transform: scale(1);
transition: .2s;
}
#age-verify .window button.back {
display: block;
float: none;
margin: auto;
background-color: #fff;
color: #ED6A5A !important;
margin-top: 20px;
}
#age-verify .window button.yes {
float: left;
}
#age-verify .window button.no {
float: right;
}
#age-verify .window button:hover {
-webkit-transform: scale(1.1);
transform: scale(1.1);
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
background-color: #f29488;
}
#age-verify .window .underBox {
position: absolute;
width: 400px;
height: 250px;
padding: 40px;
top: 100%;
left: 0;
right: 0;
background-color: #ED6A5A;
transition: 500ms;
box-sizing: border-box;
}
#age-verify .window .underBox * {
color: #FFF !important;
}
#age-verify.hidden {
opacity: 0;
visibility: hidden;
}
#age-verify.hidden .window {
-webkit-transform: scale(0.5);
transform: scale(0.5);
}
#age-verify.under .window .underBox {
top: 0%;
}
<div id="age-verify">
<div class="window"><span class="title">Are you over 18?</span> <span>To visit our website, you must be of legal drinking age.</span> <button class="yes" onclick="overAge()">Yes</button> <button class="no" onclick="underAge()">No</button>
<div class="underBox"><span class="title">Sorry!</span> <span>You need to be at least 18 to visit our website.</span> <button class="back" onclick="parent.location='https://www.google.dk/'">Go Back</button></div>
<span> </span></div>
</div>
我想添加 cookie 在加载时仅显示一次弹出窗口,并且 cookie 在一小时后过期
我尝试了很多文章,但我可能知道如何以正确的方式设置它
https://www.thepolyglotdeveloper.com/2018/02/create-email-subscription-popup-jquery/
任何人都可以为我解决它:)
解决方案
您可以使用我在代码末尾添加的这 2 个函数来获取和设置 cookie。如您所说,cookie 将在 60 分钟后过期。当页面加载时,我们检查是否可以获得我们想要的 cookie。如果可以,那么我们将隐藏弹出窗口,否则它将显示出来。
/*
You'll probably want to drop a cookie so this doesn't pop up everytime. I'd recommend this plugin:
https://github.com/js-cookie/js-cookie
*/
if(getCookie("age") != "")
{
$('#age-verify').addClass('hidden');
}
overAge = function () {
setCookie("age", "overAge");
$('#age-verify').addClass('hidden');
}
underAge = function () {
$('#age-verify').addClass('under');
}
goBack = function () {
window.history.back();
}
function setCookie(cname, cvalue) {
var d = new Date();
d.setTime(d.getTime() + (60 * 1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
#age-verify {
position: fixed;
z-index: 9997;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.6);
transition: 500ms;
}
#age-verify .window {
position: absolute;
top: 50%;
left: 50%;
width: 400px;
height: 250px;
overflow: hidden;
padding: 40px;
margin-left: -200px;
margin-top: -125px;
background-color: #fff;
border: 6px solid #ED6A5A;
box-sizing: border-box;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
transition: 500ms;
z-index: 9998;
}
#age-verify .window span {
display: block;
text-align: center;
margin-bottom: 10px;
font-family: "Source Sans Pro", sans-serif;
}
#age-verify .window span.title {
color: #ED6A5A;
font-size: 24px;
}
#age-verify .window button {
border: 0;
margin: 0;
padding: 0;
width: 48%;
height: 60px;
color: #FFF;
font-size: 18px;
background-color: #ED6A5A;
margin-top: 20px;
font-family: "Source Sans Pro", sans-serif;
-webkit-transform: scale(1);
transform: scale(1);
transition: .2s;
}
#age-verify .window button.back {
display: block;
float: none;
margin: auto;
background-color: #fff;
color: #ED6A5A !important;
margin-top: 20px;
}
#age-verify .window button.yes {
float: left;
}
#age-verify .window button.no {
float: right;
}
#age-verify .window button:hover {
-webkit-transform: scale(1.1);
transform: scale(1.1);
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
background-color: #f29488;
}
#age-verify .window .underBox {
position: absolute;
width: 400px;
height: 250px;
padding: 40px;
top: 100%;
left: 0;
right: 0;
background-color: #ED6A5A;
transition: 500ms;
box-sizing: border-box;
}
#age-verify .window .underBox * {
color: #FFF !important;
}
#age-verify.hidden {
opacity: 0;
visibility: hidden;
}
#age-verify.hidden .window {
-webkit-transform: scale(0.5);
transform: scale(0.5);
}
#age-verify.under .window .underBox {
top: 0%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="age-verify">
<div class="window"><span class="title">Are you over 18?</span> <span>To visit our website, you must be of legal drinking age.</span> <button class="yes" onclick="overAge()">Yes</button> <button class="no" onclick="underAge()">No</button>
<div class="underBox"><span class="title">Sorry!</span> <span>You need to be at least 18 to visit our website.</span> <button class="back" onclick="parent.location='https://www.google.dk/'">Go Back</button></div>
<span> </span></div>
</div>
推荐阅读
- r - 使用 purrr:map 从当前工作目录的列表中写入 xlsx 文件
- javascript - Webpack 抛出“非法导入声明”,疑似配置错误
- amazon-web-services - 如何使用 CloudFormation 中的访问密钥和密钥保护 AWS API Gateway?
- python - 用于多类分类的 TensorFlow 混淆矩阵
- ruby-on-rails - 响应前端以明文形式发送 https
- javascript - 使用 JSContext 在 Swift 中从 JavaScript 捕获打印语句
- ui-automation - 使用 UI 自动化从窗格(窗口)读取 Internet Explorer DOM
- javascript - 在 JavaScript 中,字符串化的数字是否应该在减去它们之前转换为整数?(最佳实践)
- ios - 无法使用 Xamarin.Forms WebView 进行 Dropbox 身份验证
- jquery - 如何遍历具有相同类的多个 div 并从 jQuery 中的选择中设置不同的输入字段值