Created
September 16, 2016 16:01
-
-
Save alvises/6d5ce8ce3472c2832294bd9ba0a44d2c to your computer and use it in GitHub Desktop.
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
function GeoIP(forcedIP) { | |
var locateUrl = "http://geoip.poeticoding.com/locate"; | |
//locate function, success or fail callbacks will be triggered | |
//once the request to the server is finished | |
this.locate = function(successCallback,failCallback) { | |
var xhr = new XMLHttpRequest(); | |
if(forcedIP) locateUrl += "?ip="+forcedIP; | |
xhr.open("GET", locateUrl, true); | |
xhr.timeout = 60000; | |
xhr.onload = function (e) { | |
if (xhr.readyState === 4) { | |
json = JSON.parse(xhr.responseText); | |
if(successCallback) successCallback(json.ip,json.location); | |
} else { | |
console.error(xhr.statusText); | |
} | |
} | |
xhr.ontimeout = function () { | |
if(failCallback) failCallback(); | |
} | |
xhr.send(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment