window.regions = document.querySelectorAll('link[rel=region]'); window.regionMap = {}; window.regions.forEach(function(item) { window.regionMap[item.attributes.regionflag.value] = item.attributes.href.value; }); let xhr = new XMLHttpRequest(); xhr.open("GET", "https://api.ipify.org/?format=json"); xhr.addEventListener("loadend", async function (e){ let responseObj = JSON.parse(this.response); let ip = responseObj.ip; let regionData = await ipToRegion(ip); if (window.regionMap[regionData.geoplugin_countryName]) { window.location.href = window.regionMap[regionData.geoplugin_countryName] } }); xhr.send(); async function ipToRegion(ip) { const response = await fetch(`http://www.geoplugin.net/json.gp?ip=${ip}`); const data = await response.json(); return data; }