JavaScript欧盟Cookie法律横幅未删除 [英] JavaScript EU cookie law banner not removing

查看:52
本文介绍了JavaScript欧盟Cookie法律横幅未删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试向我的网站添加欧盟Cookie政策标语.下面的javascript代码会插入横幅(一旦我添加了一点样式),但是当我单击"x"关闭横幅并希望将其隐藏以备将来访问时,什么也没有发生.横幅不会消失.

I've been trying to add a EU cookie policy banner to my website. The javascript code below inserts the banner (once I add a small bit of styling) but when I click on the "x" to close the banner and hopefully hide it for future visits, nothing happens. The banner doesn't disappear.

有人可以告诉我为什么这不起作用吗? Javascript不是我的强项!

Can anyone tell me why this isn't working? Javascript is not my strong point!

//Cookie banner

var dropCookie = true;                      // false disables the Cookie, allowing you to style the banner
var cookieDuration = 14;                    // Number of days before the cookie expires, and the banner reappears
var cookieName = 'complianceCookie';        // Name of our cookie
var cookieValue = 'on';                     // Value of cookie

function createDiv(){
    var bodytag = document.getElementsByTagName('body')[0];
    var div = document.createElement('div');
    div.setAttribute('id','cookie-law');
    div.innerHTML = '<p>Our website uses cookies. By continuing we assume your permission to deploy cookies, as detailed in our <a href="/privacy-cookies-policy/" rel="nofollow" title="Privacy &amp; Cookies Policy">privacy and cookies policy</a>. <a class="close-cookie-banner" href="javascript:void(0)" onclick="removeMe()"><span>X</span></a></p>';    
 // Be advised the Close Banner 'X' link requires jQuery

    // bodytag.appendChild(div); // Adds the Cookie Law Banner just before the closing </body> tag
    // or
    bodytag.insertBefore(div,bodytag.firstChild); // Adds the Cookie Law Banner just after the opening <body> tag

    document.getElementsByTagName('body')[0].className+=' cookiebanner'; //Adds a class tothe <body> tag when the banner is visible

    createCookie(window.cookieName,window.cookieValue, window.cookieDuration); // Create the cookie
}


function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000)); 
        var expires = "; expires="+date.toGMTString(); 
    }
    else var expires = "";
    if(window.dropCookie) { 
        document.cookie = name+"="+value+expires+"; path=/"; 
    }
}

function checkCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)===' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

window.onload = function(){
    if(checkCookie(window.cookieName) !== window.cookieValue){
        createDiv(); 
    }
};

function removeMe(){
    var element = document.getElementById('cookie-law');
    element.parentNode.removeChild(element);
};

谢谢!

推荐答案

函数removeMeonclick处理程序的当前范围内是undefined.要解决此问题,您应将function removeMe() {行替换为window.removeMe = function removeMe() {.

The function removeMe is undefined in the current scope of the onclick handler. To fix this, you should replace this line function removeMe() { with window.removeMe = function removeMe() {.

实时示例(带有格式化代码): https://jsfiddle.net/xnhpzto8/1/

Live example (with formatted code): https://jsfiddle.net/xnhpzto8/1/

这篇关于JavaScript欧盟Cookie法律横幅未删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆