选择使用Insites的CookieConsent

时间:2018-05-08 15:12:52

标签: javascript cookies

我在选择工作时遇到了麻烦。我想检查用户是否选择然后通过GTM加载脚本,但现在我只是提醒允许cookie或没有cookie,但无论我选择哪个链接,我都会获得警报允许cookie。我使用here中的代码作为起点。

<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.3/cookieconsent.min.css" />
<script src="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.3/cookieconsent.min.js"></script>
<script>
window.addEventListener("load", function () {
  var p;
  window.cookieconsent.initialise({
    "palette": {
      "popup": {
        "background": "rgba(255,255,255,0.9)",
        "text": "black"
      },
      "button": {
        "background": "transparent",
        "border": "white",
        "text": "black"
      }
    },
    "type": "opt-in",
    "content": {
      message: 'The message',
      dismiss: 'dismiss',
      allow: 'allow',
      link: 'Read more',
      href: 'http://example.com',

    },
    revokable: true,
    onStatusChange: function (status, chosenBefore) {
      var type = this.options.type;
      var didConsent = this.hasConsented();
      if (type == 'opt-in' && didConsent) {
        alert('allow cookies');
      } else {
        alert('no cookies');
      }
    },
    onRevokeChoice: function () {
    }
  }, function (popup) {

    p = popup;
    var output = '';
    for (var property in p) {
      output += property + ': ' + p[property] + '; ';
    }
  });
  document.getElementsByClassName("cc-dismiss").onclick = function (e) {
    p.revokeChoice();
  };
});

</script>

1 个答案:

答案 0 :(得分:0)

应该使用“状态”值。

onStatusChange: function (status, chosenBefore) {
  if (status === "allow") {
    alert('allow cookies');
  } else if (status === "deny"){
    alert('no cookies');
  }else{
    alert("Unhandled state. Denying use of cookies.");
  }
},