Skip to content

Instantly share code, notes, and snippets.

@bitbonsai
Created December 1, 2023 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitbonsai/4365cc78df930fd6d1fb3ca732e4fc72 to your computer and use it in GitHub Desktop.
Save bitbonsai/4365cc78df930fd6d1fb3ca732e4fc72 to your computer and use it in GitHub Desktop.
Get continent from serverless function in Cloudfare pages and show/hide content
// Localized content
const showContentByContinent = (continent) => {
const contentElements = document.querySelectorAll('.content')
if (['NA', 'OC', 'EU'].includes(continent)) {
contentElements.forEach(div => {
if (div.classList.contains(continent)) {
div.classList.remove('hid')
} else {
div.classList.add('hid')
}
})
} else {
contentElements.forEach(div => {
if (div.classList.contains('NA')) {
div.classList.remove('hid')
} else {
div.classList.add('hid')
}
})
}
}
fetch('/geo')
.then (response => {
if (!response.ok) {
throw new Error('Network down')
}
return response.json()
})
.then(data => {
//
//
// U S A and C A
//
// show localised content
showContentByContinent(data.continent)
})
.catch(error => {
usContent.forEach(divs => {
divs.classList.add('hid')
})
auContent.forEach(divs => {
divs.classList.remove('hid')
})
console.error(error)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment