Skip to content

Instantly share code, notes, and snippets.

@KruegerDesigns
Created November 20, 2019 03:44
Show Gist options
  • Save KruegerDesigns/c578b0b66b5c2a81b51746fda7dcaa6e to your computer and use it in GitHub Desktop.
Save KruegerDesigns/c578b0b66b5c2a81b51746fda7dcaa6e to your computer and use it in GitHub Desktop.
Redirect traffic based on IP Geo Location with api.ipstack.com
var getJSON = function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status == 200) {
callback(null, xhr.response);
} else {
callback(status);
}
};
xhr.send();
};
getJSON('http://api.ipstack.com/check?access_key=USE_YOUR_KEY', function(err, data) {
if (err != null) {
console.error(err);
} else {
if (data.country_code != "US") {
window.location.replace("http://domain.com/world");
} else {
window.location.replace("http://domain.com/us");
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment