JS - Cookie 管理

JS - Cookie management

我用 JS 编写了这个小代码来禁用 cookies:

$(document).ready(function() {
  var cookie_settings = getCookie("cookie-settings"); //Main cookie which contains cookie preferences
  var cookie_selector = document.getElementById("cookie-selector"); //Modal for cookie selection
  var g_recaptcha = document.getElementById("cookie-g-recaptcha"); //Example checkbox cookie
  var g_tag_manager = document.getElementById("cookie-g-tag-manager"); //Example checkbox cookie
  var messenger_plugin = document.getElementById("cookie-fb-mccp"); //Example checkbox cookie
  var g_analytics = document.getElementById("cookie-g-analytics"); //Example checkbox cookie
  var cookie_set = document.getElementById("cookie-set"); //Button to save preferences
  if (cookie_settings == null) { //Check if main cookie exist
    $(cookie_selector).modal({
      backdrop: 'static',
      keyboard: false
    }); //If not exist, open cookie selector modal
  } else {
    var cookie_settings_raw_values = getCookie("cookie-settings"); //read and save main cookie in var
    var cookie_settings_values = cookie_settings_raw_values.split('&'); //save main cookie content in array
    if (cookie_settings_values.includes(g_recaptcha.id)) {
      //If array contains recaptcha example include it
      //for example append in head -> $('head').append('myscript');
    }
    if (cookie_settings_values.includes(g_tag_manager.id)) {
      //same
      //for example append in head -> $('head').append('myscript');
    }
    if (cookie_settings_values.includes(messenger_plugin.id)) {
      //same
      //for example append in head -> $('head').append('myscript');
    }
    if (cookie_settings_values.includes(g_analytics.id)) {
      //same
      //for example append in head -> $('head').append('myscript');
    }
    //or you can remove else condition and manage this part from php
  }
  $(cookie_set).click(function() { //on save preferences click
    var selected_cookies = [g_recaptcha.id, g_tag_manager.id]; //make array and include required cookies
    if (messenger_plugin.checked == true) {
      //if messenger plugin example checkbox is checked push it's reference in array
      selected_cookies.push(messenger_plugin.id);
    }
    if (g_analytics.checked == true) {
      //same for the other optional checkboxes
      selected_cookies.push(g_analytics.id);
    }
    var expiry_date = new Date();
    expiry_date.setMonth(expiry_date.getMonth() + 6); //expiration date 6 months in my case, you can set what you want
    document.cookie = document.cookie = "cookie-settings=" + selected_cookies.join('&') + "; expires=" + expiry_date.toGMTString(); //make main cookie with required and optional selected checkboxes (the deadline is 6 months after the creation of the cookie)
    location.reload(); //reload page
  });
  //get cookie by name
  function getCookie(name) {
    var document_cookie = document.cookie;
    var prefix = name + "=";
    var begin = document_cookie.indexOf("; " + prefix);
    if (begin == -1) {
      begin = document_cookie.indexOf(prefix);
      if (begin != 0) {
        return null;
      }
    } else {
      begin += 2;
      var end = document.cookie.indexOf(";", begin);
      if (end == -1) {
        end = document_cookie.length;
      }
    }
    return decodeURI(document_cookie.substring(begin + prefix.length, end));
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

我的问题是禁用第三方 cookie 就足够了吗?

不包括脚本如果用户不接受cookies,存储的会不会没用了?该网站是否符合 GDPR?

如果不是,您是否有任何其他有效的替代方案来提议不使用第三方代码?

大多数试图遵守 GDPR 的网站默认情况下都不会加载这些脚本中的任何一个(您可能会这样做)。首先,如果用户想要加载,例如,他们会显示一个弹出窗口。跟踪 cookie,如果用户同意,它们将被加载。应加载哪些服务的配置设置/用户选择的内容将存储在 cookie 中或例如localStorage.

是的,当我们查看您加载外部脚本的方法时,您的网站似乎符合 GDPR。

如果你要删除它们,请在今天之前重新设置到期日期。