Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

63
Views
Check whether a specific cookie I created is present and to execute the next function if it is not for my cookie bar notice

I'm new to javascript and I've been trying to program a cookie bar notice.

I have learnt how to use javascript to insert the cookie bar's html into the page and how to create a cookie with the name/value pair (CookieConsent=true) once the accept button is clicked.

But I have gotten stuck on - checking whether the specific cookie (CookieConsent=true) is present once the page has loaded and to do the following:

  • if cookie is present, do not trigger the insert cookie bar html function
  • if cookie is not present, trigger the insert cookie bar html function

Im trying to make sure that the cookie check function is triggered when the page has loaded and have chosen to go with the EventListener - DOMContentLoaded for now.

Here is a basic example to illustrate my codes layout:

// When DOM has loaded trigger cookieCheck function
document.addEventListener('DOMContentLoaded', cookieCheck);

// Checks whether cookie is present and whether to execute cookiebar function or not
function cookieCheck() {
  
}

// Insert HTML for cookiebar notice and accept button into page
function cookiebar() {
  // The accept button triggers the create cookie function with  
  // a name/value pair of CookieConsent=true
}

// Creates consent cookie
function createCookie() {

}

I'm not going to include the code I've attempted to write for my problem as it is a complete mess which makes no sense. Sorry.

I've been using w3schools javascript cookies tutorial and have found many other examples on the internet for how to set, get and check cookies. But for my code, I'm completely stuck.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can try it with this solution:

// When DOM has loaded trigger cookieCheck function
document.addEventListener('DOMContentLoaded', cookieCheck);

// Checks whether cookie is present and whether to execute cookiebar function or not
function cookieCheck() {
  const cookies = getCookies();
  if(cookies.CookieConsent) {
    /**
    * Do your stuff when cookie is set
    **/
    
    cookiebar();
  } else {
    /**
    * Do your stuff when cookie is NOT set
    **/
  }
}

// Insert HTML for cookiebar notice and accept button into page
function cookiebar() {
  /**
  * Function for CookieBar insertion
  **/
}

// Creates consent cookie
function createCookie(name, value) {
  document.cookie = [name, value].join('=');
}

function getCookies() {

  let cookies = {};

  document.cookie.split(";").forEach((cookie) => {
      cookie = cookie.trim().split("=");
      let name = cookie[0];
      let val = cookie[1];
      cookies[name] = val;
  });

  return cookies;
  
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!