Hi Friends!
I just coded a custom Cookie Banner which I like to integrate into a Wordpress website. I use the custom HTML CSS and JS Snippets to integrate the code into Wordpress.
When I tested the Cookie Banner in VSCode everything worked fine, but when I integrate the code into Wordpress, I get the following error:
Uncaught TypeError: Cannot read properties of null (reading ‘addEventListener’)
The Cookie Popup doesn´t show up because the JS file does not get exectued. Probably beacuse reading ‘AddEventListener’ is null and the code stops running there?
Can someone help me with this please??
This is the JS code:
let cookieModal = document.querySelector('.cookie-consent-modal');
let cancelCookieBtn = document.querySelector('.btn.cancel');
let acceptCookieBtn = document.querySelector('.btn.accept');
cancelCookieBtn.addEventListener('click', function () {
cookieModal.classList.remove('active');
console.log('Tracking code is NOT running!');
});
acceptCookieBtn.addEventListener('click', function () {
cookieModal.classList.remove('active');
localStorage.setItem('cookieAccepted', 'yes');
useGA();
console.log('GA Code is running!2');
useFP();
console.log('FP Code is running!2');
});
setTimeout(function () {
let cookieAccepted = localStorage.getItem('cookieAccepted');
if (cookieAccepted != 'yes') {
cookieModal.classList.add('active');
}
}, 2000);
let cookieAccepted = localStorage.getItem('cookieAccepted');
if (cookieAccepted == 'yes') {
}
// Run Google Analytics Code
function useGA() {
***GOOGLE ANALYTICS CODE***
console.log('GA Code is running!1');
}
// Run Facebook Pixel Code
function useFP() {
***FACEBOOK FIXEL CODE***
console.log('FP Code is running!1');
}
What did I do wrong??