Page cover image

Code Snippets

Expand upon Geoflow's standard offerings with a little JavaScript.

Conditionally Change CSS Variable
const root = document.documentElement;

const CSSvariable = 'primary-color';

window.addEventListener('geoflowReady', function () {

    const visitorCountry = geoflow.countryName;

    // If the Visitor's Country Name Cannot Be Detected, Stop the Script
    
    if (!visitorCountry){

    console.error('Unable to detect the visitor\'s country name.');
    return;
      
    }

    if (visitorCountry === 'United States') { // If Visitor is from the US

        root.style.setProperty(`--${CSSvariable}`, '#1e88e5'); // Set the Primary Color to Blue

    } else if (visitorCountry === 'Canada') { // If Visitor is from Canada

        root.style.setProperty(`--${CSSvariable}`, '#f44336'); // Set the Primary Color to Red

    }

});
Conditionally Change Text Content
window.addEventListener('geoflowReady', function () {

  // Get the Country Name of the Visitor
  const visitorCountry = geoflow.countryName;

  // If the Visitor's Country Name Cannot Be Detected, Stop the Script
  if (!visitorCountry){

    console.error('Unable to detect the visitor\'s country name.');
    return;
      
  }

  // Add the ID for the Element You Want to Change the Text Content of
  const personalizedGreeting = document.getElementById('personalized-greeting');

  // Conditionally Change the Text Content of the Element
  switch (visitorCountry) {

    case 'Australia': // If the Visitor is in Australia
      personalizedGreeting.textContent = `G'Day Mate!`;
      break;
      
    case 'New Zealand': // If the Visitor is in New Zealand
      personalizedGreeting.textContent = `Kia ora!`;
      break;
      
    case 'Canada': // If the Visitor is in Canada
      personalizedGreeting.textContent = `How's It Goin', Eh?`;
      break;

    case 'United Kingdom': // If the Visitor is in the UK
      personalizedGreeting.textContent = `Fancy a Cuppa?`;
      break;

    default: // If the Visitor is in Any Other Country, Do Nothing
      break;

  }

});
Populate List of Provinces/States Based on Chosen Country
document.addEventListener('DOMContentLoaded', function () {

  // Add the ID for Your <select> Element Where A Country Is Chosen
  const chooseCountrySelect = document.getElementById('choose-country');

  chooseCountrySelect.addEventListener('change', function () {
    
    // Get the Chosen Country
    const chosenCountry = chooseCountrySelect.options[chooseCountrySelect.selectedIndex].value;

    switch (chosenCountry) {

      case 'australia':
        geoflow.select('#choose-state', 'au-states-and-territories', 'Choose Your State/Territory');
        break;

      case 'canada':
        geoflow.select('#choose-state', 'ca-provinces-and-territories', 'Choose Your Province/Territory');
        break;

      case 'united-states':
        geoflow.select('#choose-state', 'us-states', 'Choose Your State');
        break;

      default:
        break;

    }

  });

});

Last updated

Was this helpful?