Created
November 20, 2019 03:44
-
-
Save KruegerDesigns/c578b0b66b5c2a81b51746fda7dcaa6e to your computer and use it in GitHub Desktop.
Redirect traffic based on IP Geo Location with api.ipstack.com
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
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