Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

javascript - 我该如何做,以便每个用户(ip)都会弹出一次我的cookie同意横幅(How do I make it so that my cookie consent banner pops up once for every user(ip))

first of all, I know that a similar question has been answered before but I really don't get it, I am a nub at jquery right now and I am really not sure how to implement this on my code or get how it works.

(首先,我知道之前已经回答过类似的问题,但我确实不知道,我现在在jquery上还是个小菜一碟,我真的不确定如何在我的代码上实现它或如何工作。)

If someone could not only give me the solution but also explain what it does and how that would be fantastic.

(如果有人不仅可以给我解决方案,还可以解释它的作用,那将是多么美妙。)

So here is my HTML

(这是我的HTML)

<div id="cookieConsent">
                    This website is using cookies. <a href="#" target="_blank">More info</a>.<a class="cookieConsentOK">That's Fine</a>
</div>

Obviously I have my script file linked and the jquery CDN aswell so no need to show that, now here is my css for it.

(显然,我已链接了脚本文件和jquery CDN,因此无需显示,现在是我的CSS。)

/*Cookie Consent Begin*/
#cookieConsent {
    background-color: rgba(20,20,20,0.8);
    min-height: 26px;
    font-size: 14px;
    color: #ccc;
    line-height: 26px;
    padding: 8px 0 8px 30px;
    font-family: "Trebuchet MS",Helvetica,sans-serif;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: none;
    z-index: 9999;
    text-align: center;
}
#cookieConsent a {
    color: #4B8EE7;
    text-decoration: none;
}
#closeCookieConsent {
    display: inline-block;
    cursor: pointer;
    height: 20px;
    width: 20px;
    margin: -15px 0 0 0;
    font-weight: bold;
}
#closeCookieConsent:hover {
    color: #FFF;
}
#cookieConsent a.cookieConsentOK {
    background-color: #4B8EE7;
    color: white;
    display: inline-block;
    border-radius: 5px;
    padding: 0 20px;
    cursor: pointer;
    margin: 0 60px 0 10px;
    transition: background-color 650ms;
}
#cookieConsent a.cookieConsentOK:hover {
    background-color: #3e75bd;
}
/*Cookie Consent End*/

And finally my working but annoying jquery js file

(最后是我工作但烦人的jQuery js文件)

$(document).ready(function(){   
    setTimeout(function () {
        $("#cookieConsent").fadeIn(700);
     }, 1000);
    $(".cookieConsentOK").click(function() {
        $("#cookieConsent").fadeOut(700);
    }); 
}); 

If someone could help me with making the pop up only show once per IP or user session(and I mean like till they close their browser completely, trying to focus on mobile as well) and not only provide me with a solution but also explain it, I WOULD LOVE YOU!

(如果有人可以帮助我使弹出窗口在每个IP或用户会话中仅显示一次(我的意思是说直到他们完全关闭浏览器,然后尝试着眼于移动设备),并且不仅向我提供解决方案,而且还要向我解释,我会爱你!)

  ask by KrapnixTheFootballer translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As suggested in a comment, in the response to "cookieConsentOk" you can store the information that they OKed the cookie as a cookie or as localStorage.

(如注释中所建议,在对“ cookieConsentOk”的响应中,您可以将他们确定该cookie的信息存储为cookie或localStorage。)

With localStorage:

(使用localStorage:)

localStorage.setItem('cookies_enabled', '1'); // Use cookies

or if disabled

(或者如果被禁用)

localStorage.setItem('cookies_enabled', '0'); // No cookies

You can check this value by getting

(您可以通过获取此值来检查)

if (localStorage.getItem('cookies_enabled') === '1') {
    // save cookies for session
}

You can display the banner if:

(如果出现以下情况,则可以显示横幅:)

if (localStorage.getItem('cookies_enabled') === null) {
    // display banner
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...