Below is a JavaScript snippet that redirects based on GEOIP :
<script type="text/javascript">
// Function to call FreeGeoIP after page load
function newyorkGeoIP() {
var script = document.createElement('script');
script.src = "//freegeoip.net/json/?callback=nyGeoIP";
document.getElementsByTagName('head')[0].appendChild(script);
}
// Callback function for the GeoIP response
function nyGeoIP(d) {
if (d.country_code === 'XX') {
window.location = 'http://www.domain.com';
}
}
// Call the newyorkGeoIP function after DOM ready
document.addEventListener("DOMContentLoaded", function(event) {
newyorkGeoIP();
});
</script>
Could someone advise on how to modify this code to ensure that the FREEGEOIP server is called after the DOM has loaded?
Due to occasional unresponsiveness of the FreeGeoIP server, page rendering is delayed, sometimes causing loading times of 1-2 minutes as reflected in tools like GTmetrix and WebPageTest.