简体   繁体   English

适用于 GDPR 的 Javascript 或 JQuery 中的 Cookie

[英]Cookie in Javascript or JQuery for GDPR

I have limited experience with Javascript/JQuery outside of HTML/CSS 'animations'.在 HTML/CSS“动画”之外,我对 Javascript/JQuery 的经验有限。

I have a website that does NOT use cookies and I have a banner that displays on page load a statement to that effect.我有一个不使用 cookies 的网站,并且我有一个横幅显示在页面上加载一个声明。 Obviously this appears every time the page loads.显然,每次页面加载时都会出现这种情况。

I would like to set a cookie via an 'OK' button/link for say 60 days that is read on page load and if positive doesn't display that cookie banner.我想通过“确定”按钮/链接设置一个 cookie,例如 60 天,在页面加载时读取,如果是肯定的,则不显示该 cookie 横幅。

I've been searching the internet but I'm struggling to find what I believe should be a simple thing to implement if you know how.我一直在搜索互联网,但我很难找到我认为如果你知道如何实现的话应该是一件简单的事情。

I'm also unsure whether to use 'just' Javascript or a library (JQuery) to implement this.我也不确定是否使用'just' Javascript 或库(JQuery)来实现这一点。

Conceptually I think I need something like this:从概念上讲,我认为我需要这样的东西:

On page load, look for the cookie, if positive value, do not display the cookie banner, else display the cookie banner.在页面加载时,查找 cookie,如果为正值,则不显示 cookie 横幅,否则显示 cookie 横幅。 The 'OK' button on the cookie banner, creates the cookie with a positive value. cookie 横幅上的“确定”按钮创建具有正值的 cookie。 The cookie will expire in 60 days. cookie 将在 60 天后过期。

It's the syntax I'm struggling with.这是我正在努力解决的语法。 Can anyone help please?有人可以帮忙吗?

The functions needed = Just copy paste this in your js file we only need them to add and read cookies.所需的功能 = 只需将其复制粘贴到您的 js 文件中,我们只需要它们添加和读取 cookies。

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/; sameSite=Strict";
}
function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

Here is the part where you need to understand a bit of what is happening.这是您需要了解正在发生的事情的部分。

To set a cookie you add this line要设置 cookie,请添加此行

setCookie("cookieConsent", "YES", 60); setCookie("cookieConsent", "YES", 60);

This call the function copy pasted from earlier and set a cookie with the name cookieConsent and the value YES.这将调用之前粘贴的 function 副本,并设置一个名为 cookieConsent 且值为 YES 的 cookie。 The number 60 is how many days the cookies should store.数字 60 是 cookies 应该存储多少天。

So if you can pinpoint the place where you need to set the cookie, then just copy paste that code in there.因此,如果您可以确定需要设置 cookie 的位置,那么只需复制粘贴该代码即可。

Reading said cookie.阅读说饼干。 We can always check if a user has the cookies by this我们总是可以通过这个检查用户是否有 cookies

if(getCookie("cookieConsent") === "YES"){
    console.log("They said already said yes, dont show banner")
}

The line with console.log(), can be replaced with whatever desired function you wish to use.带有 console.log() 的行可以替换为您希望使用的任何所需的 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM