最简单的方法是使用现有的第三方工具,如
Cookie Consent
它是免费和开放源码的。我和他们没有关系,但只是觉得这样做会让过程更容易。
下面的示例将在允许Cookie之前插入一个请求opt-in权限的基本Cookie同意弹出窗口。如果您的目标是确保GDPR符合性,则需要确保跟踪脚本、cookies等在
//disable cookies
只有当用户选择加入时才设置和-
//enable cookies
.
<html>
<head>
<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>
</head>
<body>
</body>
</html>
window.addEventListener("load", function() {
window.cookieconsent.initialise({
onInitialise: function(status) {
var type = this.options.type;
var didConsent = this.hasConsented();
if (type == "opt-in" && didConsent) {
// enable cookies
}
if (type == "opt-out" && !didConsent) {
// disable cookies
}
},
onStatusChange: function(status, chosenBefore) {
var type = this.options.type;
var didConsent = this.hasConsented();
if (type == "opt-in" && didConsent) {
// enable cookies
}
if (type == "opt-out" && !didConsent) {
// disable cookies
}
},
onRevokeChoice: function() {
var type = this.options.type;
if (type == "opt-in") {
// disable cookies
}
if (type == "opt-out") {
// enable cookies
}
},
palette: {
popup: {
background: "#252e39"
},
button: {
background: "#14a7d0"
}
},
showLink: false,
type: "opt-in"
});
});
Codepen