我试图设置一个默认值“false”时,有人第一次进入网站。然后,一旦单击按钮,它将更新。这个很好用。但问题是,当他们刷新或返回页面时,无论如何都会重置回“false”。我已经把所有的饼干设置为一年后过期。我不知道我做错了什么?
require(['jquery', 'jquery/ui','Magento_Ui/js/modal/modal'], function($){
jQuery(document).ready( function() {
$("html, body").animate({ scrollTop: 0 }, "slow");
var pairs = document.cookie.split(";");
var cookies = {};
for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i].split("=");
cookies[(pair[0] + '').trim()] = unescape(pair.slice(1).join('='));
}
let check_cookie = cookies.cookie_consent;
if (check_cookie !== null && check_cookie !== undefined) {
let checkCookies = check_cookie.split(",");
for (var i = 0; i < checkCookies.length; i++) {
let cookieTags = checkCookies[i];
let tagName = cookieTags.substring(0, cookieTags.indexOf('='));
let tagValue = cookieTags.substr(cookieTags.indexOf("=") + 1);
let tag = "#"+ tagName;
if(tagValue === 'true'){
jQuery(tag).prop('checked', true);
} else {
jQuery(tag).prop('checked', false);
}
}
}
$('.open-model-button').click(function () {
$('#popup-modal').modal('openModal');
$('.modal-footer').hide();
});
});
});
require(['jquery', 'mage/cookies'], function ($) {
jQuery.cookie('rlx-consent', 'false', { expires: 365, path: '/', domain: "davidfairclough.com" });
});
然后我还有每个按钮点击的代码
function accept() {
jQuery.cookie('rlx-consent', 'true', { expires: 365, path: '/', domain: "davidfairclough.com" });
}
function acceptSelected() {
let google_analytics = document.getElementsByName('google_analytics');
let gtm = document.getElementsByName('gtm');
let rolex = document.getElementsByName('rolex');
let cookieCmp = 'google_analytics=' + google_analytics[0].checked + "%" + 'gtm=' + gtm[0].checked + "%" + 'rolex=' + rolex[0].checked;
jQuery.cookie('cookie_consent', cookieCmp, { expires: 365, path: '/', domain: "davidfairclough.com" });
jQuery.cookie('rlx-consent', 'true', { expires: 365, path: '/', domain: "davidfairclough.com" });
}
function decline() {
jQuery.cookie('rlx-consent', 'false', { expires: 365, path: '/', domain: "davidfairclough.com" });
}
1条答案
按热度按时间7eumitmz1#
在初始化cookie之前测试它是否已经存在。