{"id":1015,"date":"2020-12-20T11:03:00","date_gmt":"2020-12-20T09:03:00","guid":{"rendered":"https:\/\/html-online.com\/articles\/?p=1015"},"modified":"2026-03-09T15:41:29","modified_gmt":"2026-03-09T13:41:29","slug":"cookie-warning-widget-with-javascript","status":"publish","type":"post","link":"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/","title":{"rendered":"Cookie Warning Widget With JavaScript"},"content":{"rendered":"<p><strong>Display Cookie Compliancy warnings to website visitors in the European Union according to the <a href=\"\/articles\/general-data-protection-regulation-gdpr\/\">GDPR<\/a> and other mindless laws. Since we don&#8217;t wan&#8217;t to annoy everyone just the necessary European population we can target the visitors according to their time zones.<\/strong><\/p>\n<p class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/html-online.com\/demo\/cookie-consent\/cookie-compliancy-widget.jpg\" alt=\"cookie warning widget\"><br \/>\n<em>This label makes more sense because with a chainsaw you can actually hurt yourself.<\/em><\/p>\n<p>See the warning widget in action on this website or on <a href=\"\/demo\/cookie-consent\/\" target=\"_blank\" rel=\"noopener noreferrer\"><strong>the demo page<\/strong><\/a>.<\/p>\n<p><!--more--><br \/>\nThe warning stripe will keep showing up on every page where the script is active until the visitor clicks the accept button. This will create a cookie on the guest&#8217;s computer and keep it for 90 days even if he leaves the website.<\/p>\n<h2>What Are These Cookie Warnings Anyway?<\/h2>\n<p>You&#8217;ve probably come across many sites that seem strangely concerned about cookie education. They show redundant and ineffectual pop-ups that warns you that yes, the site uses cookies.<\/p>\n<p>If you know what cookies are aware that yes, every site uses cookies so this is not a news. However if you are not a programmer and you don&#8217;t know what cookies are (just like the creators of this stupid <a href=\"https:\/\/en.wikipedia.org\/wiki\/European_Union_law\" target=\"_blank\" rel=\"external nofollow noopener noreferrer\">law<\/a>) then the warning will just leave you confused.<\/p>\n<p>If we need to show these messages and ask for the visitor&#8217;s consent to use cookies then why don&#8217;t we need to ask their permission to run scripts, download the pages and other essential parts of the operation of the website?<\/p>\n<h2>The HTML Code<\/h2>\n<pre style=\"color:#000000;background:#ffffff;\"><span style=\"color:#a65700; \">&lt;<\/span><span style=\"color:#800000; font-weight:bold; \">div<\/span><span style=\"color:#274796; \"> <\/span><span style=\"color:#074726; \">id<\/span><span style=\"color:#808030; \">=<\/span><span style=\"color:#0000e6; \">\"myCookieConsent\"<\/span><span style=\"color:#a65700; \">&gt;<\/span>\n\t<span style=\"color:#a65700; \">&lt;<\/span><span style=\"color:#800000; font-weight:bold; \">a<\/span><span style=\"color:#274796; \"> <\/span><span style=\"color:#074726; \">id<\/span><span style=\"color:#808030; \">=<\/span><span style=\"color:#0000e6; \">\"cookieButton\"<\/span><span style=\"color:#a65700; \">&gt;<\/span>Understood<span style=\"color:#a65700; \">&lt;\/<\/span><span style=\"color:#800000; font-weight:bold; \">a<\/span><span style=\"color:#a65700; \">&gt;<\/span>\n\t<span style=\"color:#a65700; \">&lt;<\/span><span style=\"color:#800000; font-weight:bold; \">div<\/span><span style=\"color:#a65700; \">&gt;<\/span>This<span style=\"color:#074726; \">&amp;<\/span><span style=\"color:#074726; \">nbsp<\/span><span style=\"color:#074726; \">;<\/span>website is<span style=\"color:#074726; \">&amp;<\/span><span style=\"color:#074726; \">nbsp<\/span><span style=\"color:#074726; \">;<\/span>using<span style=\"color:#074726; \">&amp;<\/span><span style=\"color:#074726; \">nbsp<\/span><span style=\"color:#074726; \">;<\/span>cookies<span style=\"color:#008c00; \">.<\/span> <span style=\"color:#a65700; \">&lt;<\/span><span style=\"color:#800000; font-weight:bold; \">a<\/span><span style=\"color:#274796; \"> <\/span><span style=\"color:#074726; \">href<\/span><span style=\"color:#808030; \">=<\/span><span style=\"color:#0000e6; \">\"https:\/\/html-online.com\/privacy-terms-and-conditions\/\"<\/span><span style=\"color:#a65700; \">&gt;<\/span>More<span style=\"color:#074726; \">&amp;<\/span><span style=\"color:#074726; \">nbsp<\/span><span style=\"color:#074726; \">;<\/span>details<span style=\"color:#a65700; \">&lt;\/<\/span><span style=\"color:#800000; font-weight:bold; \">a<\/span><span style=\"color:#a65700; \">&gt;<\/span><span style=\"color:#a65700; \">&lt;\/<\/span><span style=\"color:#800000; font-weight:bold; \">div<\/span><span style=\"color:#a65700; \">&gt;<\/span>\n<span style=\"color:#a65700; \">&lt;\/<\/span><span style=\"color:#800000; font-weight:bold; \">div<\/span><span style=\"color:#a65700; \">&gt;<\/span>\n<\/pre>\n<p>This code needs to be added to every <a href=\"\/articles\/category\/html\/\">HTML<\/a> source where you wish to display the stripe. Add your own text and privacy policy link but make sure to use these ID attributes.<\/p>\n<h2>CSS Styles<\/h2>\n<p>The stylesheet contains the most essential part to stick the semi-tranparent stripe to the bottom of the page but you can adjust the colors and other aspects to fit your own design.<\/p>\n<pre style=\"color:#000000;background:#ffffff;\"><span style=\"color:#808030; \">#<\/span>myCookieConsent <span style=\"color:#800080; \">{<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">z-index<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#008c00; \">999<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">min-height<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#008c00; \">20<\/span><span style=\"color:#006600; \">px<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">font-family<\/span><span style=\"color:#808030; \">:<\/span> OpenSans<span style=\"color:#808030; \">,<\/span> arial<span style=\"color:#808030; \">,<\/span> <span style=\"color:#800000; \">\"<\/span><span style=\"color:#0000e6; \">sans-serif<\/span><span style=\"color:#800000; \">\"<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">padding<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#008c00; \">10<\/span><span style=\"color:#006600; \">px<\/span> <span style=\"color:#008c00; \">20<\/span><span style=\"color:#006600; \">px<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">background<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#400000; \">rgba<\/span><span style=\"color:#808030; \">(<\/span><span style=\"color:#008c00; \">0<\/span><span style=\"color:#808030; \">,<\/span><span style=\"color:#008c00; \">0<\/span><span style=\"color:#808030; \">,<\/span><span style=\"color:#008c00; \">0<\/span><span style=\"color:#808030; \">,<\/span><span style=\"color:#008c00; \">0.6<\/span><span style=\"color:#808030; \">)<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">overflow<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#074726; \">hidden<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">position<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#074726; \">fixed<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">color<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#008c00; \">#<\/span><span style=\"color:#008000; \">FFF<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">bottom<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#008c00; \">0<\/span><span style=\"color:#006600; \">px<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">right<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#008c00; \">10<\/span><span style=\"color:#006600; \">px<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">display<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#074726; \">none<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">left<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#008c00; \">0<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">text-align<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#074726; \">center<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">font-size<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#008c00; \">15<\/span><span style=\"color:#006600; \">px<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">font-weight<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#074726; \">bold<\/span><span style=\"color:#800080; \">;<\/span>\n<span style=\"color:#800080; \">}<\/span>\n<span style=\"color:#808030; \">#<\/span>myCookieConsent <span style=\"color:#800000; font-weight:bold; \">div<\/span> <span style=\"color:#800080; \">{<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">padding<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#008c00; \">5<\/span><span style=\"color:#006600; \">px<\/span> <span style=\"color:#008c00; \">0<\/span> <span style=\"color:#008c00; \">0<\/span><span style=\"color:#800080; \">;<\/span>\n<span style=\"color:#800080; \">}<\/span>\n<span style=\"color:#808030; \">#<\/span>myCookieConsent <span style=\"color:#800000; font-weight:bold; \">a<\/span> <span style=\"color:#800080; \">{<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">color<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#008c00; \">#<\/span><span style=\"color:#008000; \">ffba55<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">display<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#074726; \">inline-block<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">padding<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#008c00; \">0<\/span> <span style=\"color:#008c00; \">10<\/span><span style=\"color:#006600; \">px<\/span><span style=\"color:#800080; \">;<\/span>\n<span style=\"color:#800080; \">}<\/span>\n<span style=\"color:#808030; \">#<\/span>myCookieConsent <span style=\"color:#800000; font-weight:bold; \">a<\/span><span style=\"color:#800080; \">:<\/span><span style=\"color:#800000; font-weight:bold; \">hover<\/span> <span style=\"color:#800080; \">{<\/span>\n\t<span style=\"color:#bb7977; font-weight:bold; \">color<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#008c00; \">#<\/span><span style=\"color:#008000; \">fda016<\/span><span style=\"color:#800080; \">;<\/span>\n<span style=\"color:#800080; \">}<\/span>\n<span style=\"color:#808030; \">#<\/span>myCookieConsent <span style=\"color:#800000; font-weight:bold; \">a<\/span><span style=\"color:#808030; \">#<\/span>cookieButton <span style=\"color:#800080; \">{<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">display<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#074726; \">inline-block<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">color<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#008c00; \">#<\/span><span style=\"color:#008000; \">000000<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">font-size<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#008c00; \">1.1<\/span><span style=\"color:#006600; \">em<\/span><span style=\"color:#800080; \">;<\/span>\n\t<span style=\"color:#bb7977; font-weight:bold; \">background<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#008c00; \">#<\/span><span style=\"color:#008000; \">ffba55<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">text-decoration<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#074726; \">none<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">cursor<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#074726; \">pointer<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">padding<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#008c00; \">2<\/span><span style=\"color:#006600; \">px<\/span> <span style=\"color:#008c00; \">20<\/span><span style=\"color:#006600; \">px<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">float<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#074726; \">right<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">border-radius<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#008c00; \">20<\/span><span style=\"color:#006600; \">px<\/span><span style=\"color:#800080; \">;<\/span>\n<span style=\"color:#800080; \">}<\/span>\n<span style=\"color:#808030; \">#<\/span>myCookieConsent <span style=\"color:#800000; font-weight:bold; \">a<\/span><span style=\"color:#808030; \">#<\/span>cookieButton<span style=\"color:#800080; \">:<\/span><span style=\"color:#800000; font-weight:bold; \">hover<\/span> <span style=\"color:#800080; \">{<\/span>\n    <span style=\"color:#bb7977; font-weight:bold; \">background<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#008c00; \">#<\/span><span style=\"color:#008000; \">fda016<\/span><span style=\"color:#800080; \">;<\/span>\n\t<span style=\"color:#bb7977; font-weight:bold; \">color<\/span><span style=\"color:#808030; \">:<\/span> <span style=\"color:#008c00; \">#<\/span><span style=\"color:#008000; \">000<\/span><span style=\"color:#800080; \">;<\/span>\n<span style=\"color:#800080; \">}<\/span>\n<\/pre>\n<h2>The JavaScript Code<\/h2>\n<p>The engine of the widget that fades in the compliancy message if the visitor&#8217;s time zone is in the European Union and hasn&#8217;t accepted the cookies before. If the visitor accepts the use of cookies then the script saves the setting and won&#8217;t annoy him\/her again for 90 days (7776000000 miliseconds). The script is using <a href=\"https:\/\/htmlcheatsheet.com\/jquery\/\" target=\"_blank\" rel=\"noopener noreferrer\">jQuery<\/a>.<\/p>\n<pre style=\"color:#000000;background:#ffffff;\"><span style=\"color:#696969; \">\/\/ Cookie Compliancy BEGIN<\/span>\n<span style=\"color:#800000; font-weight:bold; \">function<\/span> GetCookie<span style=\"color:#808030; \">(<\/span>name<span style=\"color:#808030; \">)<\/span> <span style=\"color:#800080; \">{<\/span>\n  <span style=\"color:#800000; font-weight:bold; \">var<\/span> arg<span style=\"color:#808030; \">=<\/span>name<span style=\"color:#808030; \">+<\/span><span style=\"color:#800000; \">\"<\/span><span style=\"color:#0000e6; \">=<\/span><span style=\"color:#800000; \">\"<\/span><span style=\"color:#800080; \">;<\/span>\n  <span style=\"color:#800000; font-weight:bold; \">var<\/span> alen<span style=\"color:#808030; \">=<\/span>arg<span style=\"color:#808030; \">.<\/span><span style=\"color:#800000; font-weight:bold; \">length<\/span><span style=\"color:#800080; \">;<\/span>\n  <span style=\"color:#800000; font-weight:bold; \">var<\/span> clen<span style=\"color:#808030; \">=<\/span>document<span style=\"color:#808030; \">.<\/span>cookie<span style=\"color:#808030; \">.<\/span><span style=\"color:#800000; font-weight:bold; \">length<\/span><span style=\"color:#800080; \">;<\/span>\n  <span style=\"color:#800000; font-weight:bold; \">var<\/span> i<span style=\"color:#808030; \">=<\/span><span style=\"color:#008c00; \">0<\/span><span style=\"color:#800080; \">;<\/span>\n  <span style=\"color:#800000; font-weight:bold; \">while<\/span> <span style=\"color:#808030; \">(<\/span>i<span style=\"color:#808030; \">&lt;<\/span>clen<span style=\"color:#808030; \">)<\/span> <span style=\"color:#800080; \">{<\/span>\n\t<span style=\"color:#800000; font-weight:bold; \">var<\/span> j<span style=\"color:#808030; \">=<\/span>i<span style=\"color:#808030; \">+<\/span>alen<span style=\"color:#800080; \">;<\/span>\n\t<span style=\"color:#800000; font-weight:bold; \">if<\/span> <span style=\"color:#808030; \">(<\/span>document<span style=\"color:#808030; \">.<\/span>cookie<span style=\"color:#808030; \">.<\/span><span style=\"color:#800000; font-weight:bold; \">substring<\/span><span style=\"color:#808030; \">(<\/span>i<span style=\"color:#808030; \">,<\/span>j<span style=\"color:#808030; \">)<\/span><span style=\"color:#808030; \">==<\/span>arg<span style=\"color:#808030; \">)<\/span>\n\t  <span style=\"color:#800000; font-weight:bold; \">return<\/span> <span style=\"color:#800000; \">\"<\/span><span style=\"color:#0000e6; \">here<\/span><span style=\"color:#800000; \">\"<\/span><span style=\"color:#800080; \">;<\/span>\n\ti<span style=\"color:#808030; \">=<\/span>document<span style=\"color:#808030; \">.<\/span>cookie<span style=\"color:#808030; \">.<\/span><span style=\"color:#800000; font-weight:bold; \">indexOf<\/span><span style=\"color:#808030; \">(<\/span><span style=\"color:#800000; \">\"<\/span><span style=\"color:#0000e6; \"> <\/span><span style=\"color:#800000; \">\"<\/span><span style=\"color:#808030; \">,<\/span>i<span style=\"color:#808030; \">)<\/span><span style=\"color:#808030; \">+<\/span><span style=\"color:#008c00; \">1<\/span><span style=\"color:#800080; \">;<\/span>\n\t<span style=\"color:#800000; font-weight:bold; \">if<\/span> <span style=\"color:#808030; \">(<\/span>i<span style=\"color:#808030; \">==<\/span><span style=\"color:#008c00; \">0<\/span><span style=\"color:#808030; \">)<\/span> <span style=\"color:#800000; font-weight:bold; \">break<\/span><span style=\"color:#800080; \">;<\/span>\n  <span style=\"color:#800080; \">}<\/span>\n  <span style=\"color:#800000; font-weight:bold; \">return<\/span> <span style=\"color:#0f4d75; \">null<\/span><span style=\"color:#800080; \">;<\/span>\n<span style=\"color:#800080; \">}<\/span>\n<span style=\"color:#800000; font-weight:bold; \">function<\/span> testFirstCookie<span style=\"color:#808030; \">(<\/span><span style=\"color:#808030; \">)<\/span><span style=\"color:#800080; \">{<\/span>\n\t<span style=\"color:#800000; font-weight:bold; \">var<\/span> offset <span style=\"color:#808030; \">=<\/span> <span style=\"color:#800000; font-weight:bold; \">new<\/span> <span style=\"color:#797997; \">Date<\/span><span style=\"color:#808030; \">(<\/span><span style=\"color:#808030; \">)<\/span><span style=\"color:#808030; \">.<\/span><span style=\"color:#800000; font-weight:bold; \">getTimezoneOffset<\/span><span style=\"color:#808030; \">(<\/span><span style=\"color:#808030; \">)<\/span><span style=\"color:#800080; \">;<\/span>\n\t<span style=\"color:#800000; font-weight:bold; \">if<\/span> <span style=\"color:#808030; \">(<\/span><span style=\"color:#808030; \">(<\/span>offset <span style=\"color:#808030; \">&gt;=<\/span> <span style=\"color:#808030; \">-<\/span><span style=\"color:#008c00; \">180<\/span><span style=\"color:#808030; \">)<\/span> <span style=\"color:#808030; \">&amp;&amp;<\/span> <span style=\"color:#808030; \">(<\/span>offset <span style=\"color:#808030; \">&lt;=<\/span> <span style=\"color:#008c00; \">0<\/span><span style=\"color:#808030; \">)<\/span><span style=\"color:#808030; \">)<\/span> <span style=\"color:#800080; \">{<\/span> <span style=\"color:#696969; \">\/\/ European time zones<\/span>\n\t\t<span style=\"color:#800000; font-weight:bold; \">var<\/span> visit<span style=\"color:#808030; \">=<\/span>GetCookie<span style=\"color:#808030; \">(<\/span><span style=\"color:#800000; \">\"<\/span><span style=\"color:#0000e6; \">cookieCompliancyAccepted<\/span><span style=\"color:#800000; \">\"<\/span><span style=\"color:#808030; \">)<\/span><span style=\"color:#800080; \">;<\/span>\n\t\t<span style=\"color:#800000; font-weight:bold; \">if<\/span> <span style=\"color:#808030; \">(<\/span>visit<span style=\"color:#808030; \">==<\/span><span style=\"color:#0f4d75; \">null<\/span><span style=\"color:#808030; \">)<\/span><span style=\"color:#800080; \">{<\/span>\n\t\t   $<span style=\"color:#808030; \">(<\/span><span style=\"color:#800000; \">\"<\/span><span style=\"color:#0000e6; \">#myCookieConsent<\/span><span style=\"color:#800000; \">\"<\/span><span style=\"color:#808030; \">)<\/span><span style=\"color:#808030; \">.<\/span>fadeIn<span style=\"color:#808030; \">(<\/span><span style=\"color:#008c00; \">400<\/span><span style=\"color:#808030; \">)<\/span><span style=\"color:#800080; \">;<\/span>\t<span style=\"color:#696969; \">\/\/ Show warning<\/span>\n\t   <span style=\"color:#800080; \">}<\/span> <span style=\"color:#800000; font-weight:bold; \">else<\/span> <span style=\"color:#800080; \">{<\/span>\n\t\t\t<span style=\"color:#696969; \">\/\/ Already accepted<\/span>\n\t   <span style=\"color:#800080; \">}<\/span>\t\t\n\t<span style=\"color:#800080; \">}<\/span>\n<span style=\"color:#800080; \">}<\/span>\n$<span style=\"color:#808030; \">(<\/span>document<span style=\"color:#808030; \">)<\/span><span style=\"color:#808030; \">.<\/span>ready<span style=\"color:#808030; \">(<\/span><span style=\"color:#800000; font-weight:bold; \">function<\/span><span style=\"color:#808030; \">(<\/span><span style=\"color:#808030; \">)<\/span><span style=\"color:#800080; \">{<\/span>\n    $<span style=\"color:#808030; \">(<\/span><span style=\"color:#800000; \">\"<\/span><span style=\"color:#0000e6; \">#cookieButton<\/span><span style=\"color:#800000; \">\"<\/span><span style=\"color:#808030; \">)<\/span><span style=\"color:#808030; \">.<\/span>click<span style=\"color:#808030; \">(<\/span><span style=\"color:#800000; font-weight:bold; \">function<\/span><span style=\"color:#808030; \">(<\/span><span style=\"color:#808030; \">)<\/span><span style=\"color:#800080; \">{<\/span>\n\t\tconsole<span style=\"color:#808030; \">.<\/span><span style=\"color:#800000; font-weight:bold; \">log<\/span><span style=\"color:#808030; \">(<\/span><span style=\"color:#800000; \">'<\/span><span style=\"color:#0000e6; \">Understood<\/span><span style=\"color:#800000; \">'<\/span><span style=\"color:#808030; \">)<\/span><span style=\"color:#800080; \">;<\/span>\n\t\t<span style=\"color:#800000; font-weight:bold; \">var<\/span> expire<span style=\"color:#808030; \">=<\/span><span style=\"color:#800000; font-weight:bold; \">new<\/span> <span style=\"color:#797997; \">Date<\/span><span style=\"color:#808030; \">(<\/span><span style=\"color:#808030; \">)<\/span><span style=\"color:#800080; \">;<\/span>\n\t\texpire<span style=\"color:#808030; \">=<\/span><span style=\"color:#800000; font-weight:bold; \">new<\/span> <span style=\"color:#797997; \">Date<\/span><span style=\"color:#808030; \">(<\/span>expire<span style=\"color:#808030; \">.<\/span><span style=\"color:#800000; font-weight:bold; \">getTime<\/span><span style=\"color:#808030; \">(<\/span><span style=\"color:#808030; \">)<\/span><span style=\"color:#808030; \">+<\/span><span style=\"color:#008c00; \">7776000000<\/span><span style=\"color:#808030; \">)<\/span><span style=\"color:#800080; \">;<\/span>\n\t\tdocument<span style=\"color:#808030; \">.<\/span>cookie<span style=\"color:#808030; \">=<\/span><span style=\"color:#800000; \">\"<\/span><span style=\"color:#0000e6; \">cookieCompliancyAccepted=here; expires=<\/span><span style=\"color:#800000; \">\"<\/span><span style=\"color:#808030; \">+<\/span>expire<span style=\"color:#808030; \">+<\/span><span style=\"color:#800000; \">\"<\/span><span style=\"color:#0000e6; \">;path=\/<\/span><span style=\"color:#800000; \">\"<\/span><span style=\"color:#800080; \">;<\/span>\n        $<span style=\"color:#808030; \">(<\/span><span style=\"color:#800000; \">\"<\/span><span style=\"color:#0000e6; \">#myCookieConsent<\/span><span style=\"color:#800000; \">\"<\/span><span style=\"color:#808030; \">)<\/span><span style=\"color:#808030; \">.<\/span>hide<span style=\"color:#808030; \">(<\/span><span style=\"color:#008c00; \">400<\/span><span style=\"color:#808030; \">)<\/span><span style=\"color:#800080; \">;<\/span>\n    <span style=\"color:#800080; \">}<\/span><span style=\"color:#808030; \">)<\/span><span style=\"color:#800080; \">;<\/span>\n\ttestFirstCookie<span style=\"color:#808030; \">(<\/span><span style=\"color:#808030; \">)<\/span><span style=\"color:#800080; \">;<\/span>\n<span style=\"color:#800080; \">}<\/span><span style=\"color:#808030; \">)<\/span><span style=\"color:#800080; \">;<\/span>\n<span style=\"color:#696969; \">\/\/ Cookie Compliancy END<\/span>\n<\/pre>\n<p>We have a <a href=\"\/articles\/cookie-consent-warning-strap-website\/\">more simple solution<\/a> for this using only HTML-CSS.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Display Cookie Compliancy warnings to website visitors in the European Union according to the GDPR and other mindless laws. Since we don&#8217;t wan&#8217;t to annoy everyone just the necessary European population we can target the visitors according to their time zones. This label makes more sense because with a chainsaw you can actually hurt yourself. &hellip; <a href=\"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Cookie Warning Widget With JavaScript&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,2,3,5],"tags":[],"class_list":["post-1015","post","type-post","status-publish","format-standard","hentry","category-articles","category-css","category-freebies","category-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Cookie Warning Widget With JavaScript - HTMLOnline.com<\/title>\n<meta name=\"description\" content=\"Display Cookie Compliancy warnings to website visitors in the European Union according to the GDPR and other mindless laws.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Cookie Warning Widget With JavaScript - HTMLOnline.com\" \/>\n<meta property=\"og:description\" content=\"Display Cookie Compliancy warnings to website visitors in the European Union according to the GDPR and other mindless laws.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"HTML Online\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/htmlcoding\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-20T09:03:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-09T13:41:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/html-online.com\/demo\/cookie-consent\/cookie-compliancy-widget.jpg\" \/>\n<meta name=\"author\" content=\"HTML Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"HTML Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/\"},\"author\":{\"name\":\"HTML Editor\",\"@id\":\"https:\/\/html-online.com\/articles\/#\/schema\/person\/019f9afa07f209153df0fecfc90b8c1d\"},\"headline\":\"Cookie Warning Widget With JavaScript\",\"datePublished\":\"2020-12-20T09:03:00+00:00\",\"dateModified\":\"2026-03-09T13:41:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/\"},\"wordCount\":389,\"publisher\":{\"@id\":\"https:\/\/html-online.com\/articles\/#organization\"},\"image\":{\"@id\":\"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/html-online.com\/demo\/cookie-consent\/cookie-compliancy-widget.jpg\",\"articleSection\":[\"Articles\",\"CSS\",\"Freebies\",\"JavaScript\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/\",\"url\":\"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/\",\"name\":\"Cookie Warning Widget With JavaScript - HTMLOnline.com\",\"isPartOf\":{\"@id\":\"https:\/\/html-online.com\/articles\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/html-online.com\/demo\/cookie-consent\/cookie-compliancy-widget.jpg\",\"datePublished\":\"2020-12-20T09:03:00+00:00\",\"dateModified\":\"2026-03-09T13:41:29+00:00\",\"description\":\"Display Cookie Compliancy warnings to website visitors in the European Union according to the GDPR and other mindless laws.\",\"breadcrumb\":{\"@id\":\"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/#primaryimage\",\"url\":\"https:\/\/html-online.com\/demo\/cookie-consent\/cookie-compliancy-widget.jpg\",\"contentUrl\":\"https:\/\/html-online.com\/demo\/cookie-consent\/cookie-compliancy-widget.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/html-online.com\/articles\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Cookie Warning Widget With JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/html-online.com\/articles\/#website\",\"url\":\"https:\/\/html-online.com\/articles\/\",\"name\":\"HTML Online Articles\",\"description\":\"Tips, tricks, tutorials\u2026\",\"publisher\":{\"@id\":\"https:\/\/html-online.com\/articles\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/html-online.com\/articles\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/html-online.com\/articles\/#organization\",\"name\":\"HTML Online\",\"url\":\"https:\/\/html-online.com\/articles\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/html-online.com\/articles\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/html-online.com\/articles\/wp-content\/uploads\/2022\/06\/logo.jpg\",\"contentUrl\":\"https:\/\/html-online.com\/articles\/wp-content\/uploads\/2022\/06\/logo.jpg\",\"width\":350,\"height\":350,\"caption\":\"HTML Online\"},\"image\":{\"@id\":\"https:\/\/html-online.com\/articles\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/htmlcoding\/\",\"https:\/\/www.linkedin.com\/in\/ferencdenes\/\",\"https:\/\/www.youtube.com\/channel\/UCn38Jw1sJzbjVHO95Zp0Sww\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/html-online.com\/articles\/#\/schema\/person\/019f9afa07f209153df0fecfc90b8c1d\",\"name\":\"HTML Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/html-online.com\/articles\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7c1d8f5e7f1dc3e261766a96ac50c6a907fa5c236e87ab73379c57c9114e92cd?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7c1d8f5e7f1dc3e261766a96ac50c6a907fa5c236e87ab73379c57c9114e92cd?s=96&d=mm&r=g\",\"caption\":\"HTML Editor\"},\"description\":\"In 2013, while wrestling with a mountain of client articles and an uncooperative CMS, I decided enough was enough. So, I created an online HTML editor purely out of necessity (and mild frustration). What began as a tool for my own sanity quickly evolved into a gift for the world\u2014or at least for anyone trying to avoid breaking their website's code. Since then, I've shared my tech notes on my blog, which serves as both a handy reference and a digital diary of the adventures and misadventures of a coder.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/ferencdenes\/\",\"https:\/\/www.youtube.com\/@htmlg\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Cookie Warning Widget With JavaScript - HTMLOnline.com","description":"Display Cookie Compliancy warnings to website visitors in the European Union according to the GDPR and other mindless laws.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/","og_locale":"en_GB","og_type":"article","og_title":"Cookie Warning Widget With JavaScript - HTMLOnline.com","og_description":"Display Cookie Compliancy warnings to website visitors in the European Union according to the GDPR and other mindless laws.","og_url":"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/","og_site_name":"HTML Online","article_publisher":"https:\/\/www.facebook.com\/htmlcoding\/","article_published_time":"2020-12-20T09:03:00+00:00","article_modified_time":"2026-03-09T13:41:29+00:00","og_image":[{"url":"https:\/\/html-online.com\/demo\/cookie-consent\/cookie-compliancy-widget.jpg","type":"","width":"","height":""}],"author":"HTML Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"HTML Editor","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/#article","isPartOf":{"@id":"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/"},"author":{"name":"HTML Editor","@id":"https:\/\/html-online.com\/articles\/#\/schema\/person\/019f9afa07f209153df0fecfc90b8c1d"},"headline":"Cookie Warning Widget With JavaScript","datePublished":"2020-12-20T09:03:00+00:00","dateModified":"2026-03-09T13:41:29+00:00","mainEntityOfPage":{"@id":"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/"},"wordCount":389,"publisher":{"@id":"https:\/\/html-online.com\/articles\/#organization"},"image":{"@id":"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/html-online.com\/demo\/cookie-consent\/cookie-compliancy-widget.jpg","articleSection":["Articles","CSS","Freebies","JavaScript"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/","url":"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/","name":"Cookie Warning Widget With JavaScript - HTMLOnline.com","isPartOf":{"@id":"https:\/\/html-online.com\/articles\/#website"},"primaryImageOfPage":{"@id":"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/#primaryimage"},"image":{"@id":"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/html-online.com\/demo\/cookie-consent\/cookie-compliancy-widget.jpg","datePublished":"2020-12-20T09:03:00+00:00","dateModified":"2026-03-09T13:41:29+00:00","description":"Display Cookie Compliancy warnings to website visitors in the European Union according to the GDPR and other mindless laws.","breadcrumb":{"@id":"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/"]}]},{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/#primaryimage","url":"https:\/\/html-online.com\/demo\/cookie-consent\/cookie-compliancy-widget.jpg","contentUrl":"https:\/\/html-online.com\/demo\/cookie-consent\/cookie-compliancy-widget.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/html-online.com\/articles\/cookie-warning-widget-with-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/html-online.com\/articles\/"},{"@type":"ListItem","position":2,"name":"Cookie Warning Widget With JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/html-online.com\/articles\/#website","url":"https:\/\/html-online.com\/articles\/","name":"HTML Online Articles","description":"Tips, tricks, tutorials\u2026","publisher":{"@id":"https:\/\/html-online.com\/articles\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/html-online.com\/articles\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/html-online.com\/articles\/#organization","name":"HTML Online","url":"https:\/\/html-online.com\/articles\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/html-online.com\/articles\/#\/schema\/logo\/image\/","url":"https:\/\/html-online.com\/articles\/wp-content\/uploads\/2022\/06\/logo.jpg","contentUrl":"https:\/\/html-online.com\/articles\/wp-content\/uploads\/2022\/06\/logo.jpg","width":350,"height":350,"caption":"HTML Online"},"image":{"@id":"https:\/\/html-online.com\/articles\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/htmlcoding\/","https:\/\/www.linkedin.com\/in\/ferencdenes\/","https:\/\/www.youtube.com\/channel\/UCn38Jw1sJzbjVHO95Zp0Sww"]},{"@type":"Person","@id":"https:\/\/html-online.com\/articles\/#\/schema\/person\/019f9afa07f209153df0fecfc90b8c1d","name":"HTML Editor","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/html-online.com\/articles\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/7c1d8f5e7f1dc3e261766a96ac50c6a907fa5c236e87ab73379c57c9114e92cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7c1d8f5e7f1dc3e261766a96ac50c6a907fa5c236e87ab73379c57c9114e92cd?s=96&d=mm&r=g","caption":"HTML Editor"},"description":"In 2013, while wrestling with a mountain of client articles and an uncooperative CMS, I decided enough was enough. So, I created an online HTML editor purely out of necessity (and mild frustration). What began as a tool for my own sanity quickly evolved into a gift for the world\u2014or at least for anyone trying to avoid breaking their website's code. Since then, I've shared my tech notes on my blog, which serves as both a handy reference and a digital diary of the adventures and misadventures of a coder.","sameAs":["https:\/\/www.linkedin.com\/in\/ferencdenes\/","https:\/\/www.youtube.com\/@htmlg"]}]}},"_links":{"self":[{"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/posts\/1015","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/comments?post=1015"}],"version-history":[{"count":3,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/posts\/1015\/revisions"}],"predecessor-version":[{"id":2555,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/posts\/1015\/revisions\/2555"}],"wp:attachment":[{"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/media?parent=1015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/categories?post=1015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/tags?post=1015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}