Created
December 1, 2023 01:05
-
-
Save bitbonsai/4365cc78df930fd6d1fb3ca732e4fc72 to your computer and use it in GitHub Desktop.
Get continent from serverless function in Cloudfare pages and show/hide content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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