Ecommerce Shopify WordPress Discussion

JavaScript Code Only Loads on Second Visit

I'm working with a WordPress site using the Divi theme. I have inserted a JavaScript code into the body via the theme options. However, I'm encountering an issue where the script only executes on the second page load. Additionally, it doesn't seem to run at all when using Incognito mode. This script is intended to create interactive tooltips that respond to user actions, enhancing the user experience on the site. For context, this script includes a tooltip function, which I've excluded from this post for brevity and focus on the main issue. Here is a relevant snippet of the code: window.onload = function() { const kreise = document.querySelectorAll('.svg-container g[id^="Kreis"]'); const infoContainer = document.getElementById('info-container'); const infoText = document.getElementById('info-text'); const svgContainer = document.querySelector('.svg-container'); kreise.forEach(kreis => { kreis.addEventListener('click', () => { const content = getTooltipContent(kreis.id); infoText.innerHTML = content; if (window.innerWidth <= 768) infoContainer.classList.add('info-container-mobile-active'); } else { infoContainer.classList.add('active'); svgContainer.classList.add('active'); } }); }); I've tried using both the DOMContentLoaded event and the jQuery ready function to address this issue, but these approaches did not resolve the problem.
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.