使用Insites的CookieConsent中的setCookie

时间:2017-06-09 11:41:19

标签: javascript cookies

我正在使用Insites的CookieConsent插件,并希望在“onStatusChange”方法中使用“setCookie”函数来创建其他(其他)Cookie。

onStatusChange: function(status, chosenBefore){
var type = this.options.type;
  var didConsent = this.hasConsented();

  if (type == 'opt-in' && didConsent) {
     // enable cookies


  }

}

问题是我得到了“无法读取未定义的属性'setCookie'”。任何帮助将不胜感激。

祝你好运

2 个答案:

答案 0 :(得分:0)

这是我使用的插件: [链接] https://cookieconsent.insites.com/documentation/javascript-api/

这是我的剧本:

<script>

window.addEventListener("load", function(){
    var p;
window.cookieconsent.initialise({

  "palette": {
    "popup": {
      "background": "rgba(255,150,33,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) {

    setCookie('xxx','allow',365,'/','');    

  }
  },
  cookie: {
    name: 'wwwZalarisCC'    
  },
 onRevokeChoice: function(){


 }
}, function (popup) {

    p = popup;
    var output = '';
    for (var property in p) {
  output += property + ': ' + p[property]+'; ';
}

  });
 document.getElementById('btn-revokeChoice').onclick = function (e) {

    p.revokeChoice();

  };
  });

</script>

答案 1 :(得分:0)

为了在您的onStatusChange函数中创建其他/其他cookie,请尝试如下操作:

onStatusChange: function(status, chosenBefore) {
    var type = this.options.type;
    var didConsent = this.hasConsented();

    if (type === 'opt-in' && didConsent) {
        document.cookie = 'cookie-name=cookie-value; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
    }
}

在这里您可以找到包含一些实施示例的更长的讨论:

https://github.com/insites/cookieconsent/issues/205

希望这对您有帮助!