2

I want a alert at the bottom of the screen that says that the website is using cookies (and it only needs to be shown to new users). It needs to stay at the bottom if you scroll down too. I don't know how to do it, because I am a self-learning person and I am just starting learning how to code. Thanks for the help.

Julian
  • 33
  • 1
  • 5

1 Answers1

2

Part 1: HTML & CSS

We need to add the alert panel into the bottom of the page. To style this panel, we should use CSS. This panel also has the confirmation button. When the user clicks the confirmation button, we set a cookie in the browser and never show the panel again.

<div class="bottom" id="alert">
    This website uses cookies
    <button onclick="accpetCookie()">
        Click here to accept cookies
    </button>
</div>
<div class="scroll">
    website content
</div>

Here is the styling for the CSS classes:

.bottom {
    position: fixed;
    bottom: 0;
    width: 100%;
    background: #ccc;
    color: #fff
}

.scroll {
    min-height: 1500px;
}

Part 2: JavaScript

<script>
    // if user has already checked the confirmation button
    // the alert panel should be hidden.   
    if (getCookie('accepted') === 'yes') {
        document.getElementById("alert").style.display = "none";
    }

    // user clicks the confirmation -> 
    // set a cookie with name: 'accepted' and value: 'yes'
    function accpetCookie() {
        setCookie('accepted', 'yes', 100);
    }
    

    // code from: http://stackoverflow.com/a/4825695/191220
    // set cookie method
    function setCookie(c_name, value, exdays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + exdays);

        var c_value = escape(value) + 
            ((exdays == null) 
                ? "" 
                : "; expires=" + exdate.toUTCString());

        document.cookie = c_name + "=" + c_value;
    }

    // get cookie method   
    function getCookie(c_name) {
        var i, x, y, ARRcookies = document.cookie.split(";");
        for (i = 0; i < ARRcookies.length; i++) {
            x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
            y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
            x = x.replace(/^\s+|\s+$/g, "");
            if (x == c_name) {
                return unescape(y);
            }
        }
    }
</script>

Check the JsFiddle page for a live demo.

CarenRose
  • 1,266
  • 1
  • 12
  • 24
Mironline
  • 2,755
  • 7
  • 35
  • 61
  • @Julian have you added the `` tag in html page ?. please delete your second post(answer), for adding note to any answer in this website you can use comment section – Mironline Oct 20 '16 at 14:59
  • I did it again and now there is an other problem. If I click to accept, it will come back if I refresh the page and it creates no cookie (I can't see one in my cookie inspector). – Julian Oct 20 '16 at 21:59
  • The default state of the "bottom" div should be `display:none` in the CSS to prevent it from flickering when page loads. Change `if (getCookie('accepted') === 'yes') { document.getElementById("alert").style.display = "none"; }` to `if (getCookie('accepted') !== 'yes') { document.getElementById("alert").style.display = "block"; }` – Victor Stoddard Feb 15 '19 at 22:03