$('document').ready(function () { var docCookies = { getItem: function (sKey) { return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[-.+*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null; }, setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) { if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; } var sExpires = ""; if (vEnd) { switch (vEnd.constructor) { case Number: sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd; break; case String: sExpires = "; expires=" + vEnd; break; case Date: sExpires = "; expires=" + vEnd.toUTCString(); break; } } document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : ""); return true; }, removeItem: function (sKey, sPath, sDomain) { if (!sKey || !this.hasItem(sKey)) { return false; } document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + ( sDomain ? "; domain=" + sDomain : "") + ( sPath ? "; path=" + sPath : ""); return true; }, hasItem: function (sKey) { return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[-.+*]/g, "\\$&") + "\\s*\\=")).test(document.cookie); }, keys: /* optional method: you can safely remove it! */ function () { var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/); for (var nIdx = 0; nIdx < aKeys.length; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); } return aKeys; } }; var config = { title: "Our site use cookies", content: "contentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontent", saveText: "SAVE", leftText: "Personalize cookies", rightText: "Accept", cookiePolicy: { title: 'Personalize cookies', content: [{ title: 'Essential cookies (Required)', content: 'These cookies are strictly necessary to enable access and use of our Platform. They provide necessary functionality to ensure the proper performance, security and functionality of our Platform. These cookies cannot be disabled.', disable: true }, { title: 'Analytical cookies', content: 'These cookies help us determine and understand how users interact with and use our Platform.', name: 'Analytics cookies' }, { title: 'Personalization cookies', content: 'We use these cookies to make advertising messages more relevant to you. These cookies also perform functions like preventing the same ad from continuously reappearing, ensuring that ads are properly displayed for advertisers, and in some cases selecting advertisements that are based on your interests.', name: 'Personalization cookies' }, { title: 'Marketing cookies', content: 'These cookies help us to provide you with more targeted and relevant marketing and evaluate the effectiveness of our marketing communications by monitoring open rate and conversions.', name: 'Marketing cookies' }, { title: 'Social networking cookies', content: 'We use these cookies to enable you to share pages and content that you find interesting on our Platform through third-party social networks and other websites.', name: 'Social networking cookies' }] } }; var EXPIRES = 15552000; var CLOSE_EXPIRES = 604800; var HAS_POLICY = 'hasPolicy'; var CLOSE_POLICY = 'closePolicy'; var TRUE_VAL = 'Y'; var hasShowPolicy = docCookies.getItem(HAS_POLICY) === TRUE_VAL; var closePolicy = docCookies.getItem(CLOSE_POLICY) === TRUE_VAL; if (!hasShowPolicy && !closePolicy && !document.querySelector('#cookie-policy__card')) { renderDetail(); addDetailEvents(); } function renderDetail() { $('body').append('
'); } function addDetailEvents() { $('.cookie-policy__accept-button').click(function() { config.cookiePolicy.content.filter(function (item) { return !!item.name; }).forEach(function (item) { docCookies.setItem(encodeURIComponent(item.name), TRUE_VAL, EXPIRES); }); docCookies.setItem(HAS_POLICY, TRUE_VAL, EXPIRES); $('#cookie-policy__card').remove(); }); $('.cookie-policy__close-icon').click(function() { docCookies.setItem(CLOSE_POLICY, TRUE_VAL, CLOSE_EXPIRES); $('#cookie-policy__card').remove(); }); $('.cookie-policy__personalize-button').click(function() { renderDialog(); addDialogEvents(); }); } function renderDialog() { $('body').append('\n '); } function addDialogEvents() { $('.cookie-policy__modal-close').click(function () { docCookies.setItem(CLOSE_POLICY, TRUE_VAL, CLOSE_EXPIRES); $('#cookie-policy__dialog').remove(); }); $('.cookie-policy__modal-close').click(function () { if (!docCookies.getItem(CLOSE_POLICY)) { docCookies.setItem(CLOSE_POLICY, TRUE_VAL, CLOSE_EXPIRES); } $('#cookie-policy__dialog').remove(); }); $('.cookie-policy__setting-swith-btn').click(function () { var id = $(this).attr('data-id'); $(this).toggleClass('setting-swith-checked'); if ($(this).hasClass('setting-swith-checked')) { docCookies.setItem(encodeURIComponent(id), TRUE_VAL, EXPIRES) } else { docCookies.removeItem(encodeURIComponent(id)); }; docCookies.setItem(HAS_POLICY, TRUE_VAL, EXPIRES); }); $('.cookie-policy__modal-save-btn').click(function () { docCookies.setItem(HAS_POLICY, TRUE_VAL, EXPIRES); $('#cookie-policy__dialog').remove(); $('#cookie-policy__card').remove(); }); } // 按钮的代码 给用户 同意 cookie document.addEventListener('click', function (e) { const btnAcceptCookie = e.target.closest('.cookie-policy__accept-button'); //同意按钮 if (btnAcceptCookie === null) return; const option = { analytics_storage: 'granted', ad_storage: 'granted', ad_user_data: 'granted', ad_personalization: 'granted', }; gtag('consent', 'update', option); // 3. 保存并记录用户是否同意的选项 以便下一页面自动选择是否同意 cookie localStorage['gtag.consent.option'] = JSON.stringify(option); }); });