Last active
March 7, 2018 10:17
-
-
Save cssimsek/65b54c906d21ba20d4202b80ababf1b0 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
/* | |
* Get Human Readable Location | |
* Uses https://api.ipify.org to get ip | |
* Uses https://freegeoip.net to get location info | |
*/ | |
window.getPublicIP = function(json) { | |
makeScript("https://freegeoip.net/json/"+json.ip+"?",{callback:"getRealLocation"}); | |
} | |
window.getRealLocation = function(json){ | |
console.log(JSON.stringify(json)); | |
} | |
function makeScript(host,qpObj){ | |
let _script = document.createElement("script"); | |
_script.type="application/javascript"; | |
_script.src = host; | |
if(qpObj){ | |
for(let key in qpObj){ | |
_script.src+=key+"="+qpObj[key]+"&"; | |
} | |
} | |
let firstScript = document.getElementsByTagName('script')[0]; | |
firstScript.parentNode.insertBefore(_script, firstScript); | |
} | |
//Invoke first call to fetch ip | |
makeScript("https://api.ipify.org?",{format:"jsonp",callback:"getPublicIP"}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment