需要设置Cookie,以便年龄门仅在网站上显示一次。
我试图使用CMS中的插件,但对于当前站点而言,该版本太新了。因此,我此刻迷失了方向,甚至不知道从哪里真正开始。
<script type="text/javascript">
var age__gate = document.querySelector(".age__content");
var age__gate__no = document.querySelector(".age__no");
var age__check = document.querySelector(".age__check");
var age__button__no = document.getElementById("age__No");
var age__button__yes = document.getElementById("age__Yes");
age__button__no.addEventListener("click", function() {
age__gate.classList.add("is-active");
age__gate__no.classList.add("is-active");
});
age__button__yes.addEventListener("click", function() {
age__check.classList.add("is-active");
document.cookie = 'age_confirmed=checked';
});
</script>
所以这是我到目前为止的脚本。我只需要一种方法即可将age_confirmed = checked附加到弹出窗口本身,因此它不会显示。 Cookie可以很好地存储在后端中,但是现在CMS代码不起作用,我很茫然。使该工作正常进行的任何帮助都将非常有用:)
编辑
整个CMS并未损坏,只是用于存储cookie的插件。
我觉得有些事情应该起作用;
<script type="text/javascript">
var age__gate = document.querySelector(".age__content");
var age__gate__no = document.querySelector(".age__no");
var age__check = document.querySelector(".age__check");
var age__button__no = document.getElementById("age__No");
var age__button__yes = document.getElementById("age__Yes");
age__button__no.addEventListener("click", function() {
age__gate.classList.add("is-active");
age__gate__no.classList.add("is-active");
});
age__button__yes.addEventListener("click", function() {
age__check.classList.add("is-active");
document.cookie = 'age_confirmed=checked';
if (age_confirmed == checked) {
age__check.classList.remove("is-active");
}
});
</script>
答案 0 :(得分:1)
在修复CMS /后端/您可以使用window.localStorage玩的任何东西之前-(至少在欧洲)它符合与存储Cookie相同的规定
具有此HTML:
<label><input type="checkbox" id="age">Let me in, I am 18+</label>
这是JavaScript
// Put this chunk on every page (Fails if unset or if value is "minor")
if (localStorage.age !== 'adult') alert("You're not of legal age to see this content");
// Put this chunk where needed
const EL_age = document.querySelector("#age");
EL_age.addEventListener('change', () => localStorage.age = EL_age.checked ? 'adult' : 'minor');
EL_age.checked = localStorage.getItem('age') === 'adult'; // If found - update checkbox state