html - Delete a cookie, javascript -
i'm trying conform italian cookie law banner notify cookies left website. used (and changed little bit) banner code made available google (https://www.cookiechoices.org/)
the banner works fine added little function (delete_cookie) in order let user delete manually cookie, left banner, clicking on link in cookie policy page. made tests , can't manage delete damn cookie when click on link :) (i'm working in local)
(function(window) { if (!!window.cookiechoices) { return window.cookiechoices; } var document = window.document; // ie8 not support textcontent, should fallback innertext. var supportstextcontent; try{ supportstextcontent = ('textcontent'); } catch(e){ supportstextcontent = false; } var cookiechoices = (function() { var cookiename = 'displaycookieconsent'; var cookieconsentid = 'cookiechoiceinfo'; var dismisslinkid = 'cookiechoicedismiss'; function _createheaderelement(cookietext, dismisstext, linktext, linkhref) { var butterbarstyles = 'position:fixed; font: bold 1em \'open sans\', sans-serif; color: white;width:100%;height: 70px;background-color:#666;' + 'margin:0; left:0; top:0;padding:10px;z-index:1000;text-align:center;'; var cookieconsentelement = document.createelement('div'); cookieconsentelement.id = cookieconsentid; cookieconsentelement.style.csstext = butterbarstyles; cookieconsentelement.appendchild(_createconsenttext(cookietext)); if (!!linktext && !!linkhref) { cookieconsentelement.appendchild(_createinformationlink(linktext, linkhref)); } cookieconsentelement.appendchild(_createdismisslink(dismisstext)); return cookieconsentelement; } function _createdialogelement(cookietext, dismisstext, linktext, linkhref) { var glassstyle = 'position:fixed;width:100%;height:100%;z-index:999;' + 'top:0;left:0;opacity:0.5;filter:alpha(opacity=50);' + 'background-color:#ccc;'; var dialogstyle = 'z-index:1000;position:fixed;left:50%;top:50%'; var contentstyle = 'position:relative;left:-50%;margin-top:-25%;' + 'background-color:#fff;padding:20px;box-shadow:4px 4px 25px #888;'; var cookieconsentelement = document.createelement('div'); cookieconsentelement.id = cookieconsentid; var glasspanel = document.createelement('div'); glasspanel.style.csstext = glassstyle; var content = document.createelement('div'); content.style.csstext = contentstyle; var dialog = document.createelement('div'); dialog.style.csstext = dialogstyle; var dismisslink = _createdismisslink(dismisstext); dismisslink.style.display = 'block'; dismisslink.style.textalign = 'right'; dismisslink.style.margintop = '8px'; content.appendchild(_createconsenttext(cookietext)); if (!!linktext && !!linkhref) { content.appendchild(_createinformationlink(linktext, linkhref)); } content.appendchild(dismisslink); dialog.appendchild(content); cookieconsentelement.appendchild(glasspanel); cookieconsentelement.appendchild(dialog); return cookieconsentelement; } function _setelementtext(element, text) { if (supportstextcontent) { element.textcontent = text; } else { element.innertext = text; } } function _createconsenttext(cookietext) { var consenttext = document.createelement('span'); _setelementtext(consenttext, cookietext); return consenttext; } function _createdismisslink(dismisstext) { var dismisslink = document.createelement('button'); _setelementtext(dismisslink, dismisstext); dismisslink.id = dismisslinkid; dismisslink.style.textdecoration= 'none'; dismisslink.style.color= '#666'; dismisslink.href = '#'; dismisslink.style.marginleft = '24px'; return dismisslink; } function _createinformationlink(linktext, linkhref) { var infolink = document.createelement('a'); _setelementtext(infolink, linktext); infolink.href = linkhref; infolink.target = '_blank'; infolink.style.textdecoration= 'none'; infolink.style.color= '#33cc33'; infolink.style.marginleft = '8px'; return infolink; } function _dismisslinkclick() { _saveuserpreference(); _removecookieconsent(); return false; } function _showcookieconsent(cookietext, dismisstext, linktext, linkhref, isdialog) { if (_shoulddisplayconsent()) { _removecookieconsent(); var consentelement = (isdialog) ? _createdialogelement(cookietext, dismisstext, linktext, linkhref) : _createheaderelement(cookietext, dismisstext, linktext, linkhref); var fragment = document.createdocumentfragment(); fragment.appendchild(consentelement); document.body.appendchild(fragment.clonenode(true)); document.getelementbyid(dismisslinkid).onclick = _dismisslinkclick; } } function showcookieconsentbar(cookietext, dismisstext, linktext, linkhref) { _showcookieconsent(cookietext, dismisstext, linktext, linkhref, false); } function showcookieconsentdialog(cookietext, dismisstext, linktext, linkhref) { _showcookieconsent(cookietext, dismisstext, linktext, linkhref, true); } function _removecookieconsent() { var cookiechoiceelement = document.getelementbyid(cookieconsentid); if (cookiechoiceelement != null) { cookiechoiceelement.parentnode.removechild(cookiechoiceelement); } } function _saveuserpreference() { // set cookie expiry 31 days after today. var expirydate = new date(); expirydate.setdate(expirydate.getdate() + 31); document.cookie = cookiename + '=y; expires=' + expirydate.togmtstring() +'; path=/;'; } function _shoulddisplayconsent() { // display header if cookie has not been set. return !document.cookie.match(new regexp(cookiename + '=([^;]+)')); } var exports = {}; exports.showcookieconsentbar = showcookieconsentbar; exports.showcookieconsentdialog = showcookieconsentdialog; return exports; })(); window.cookiechoices = cookiechoices; return cookiechoices; })(this); function delete_cookie(cookie_name) { var cookie_date = new date ( ); cookie_date.settime ( cookie_date.gettime() - 1 ); document.cookie = cookie_name + '=; path=/; expires=' + cookie_date.togmtstring(); }
html code:
<a href="#" onclick="delete_cookie('displaycookieconsent');" class="">clicca qui per cancellarlo</a>
Comments
Post a Comment