Skip to content

Instantly share code, notes, and snippets.

@R41D3NN
Last active May 21, 2023 00:51
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save R41D3NN/b04369e9ed38ad1519fb to your computer and use it in GitHub Desktop.
Save R41D3NN/b04369e9ed38ad1519fb to your computer and use it in GitHub Desktop.
Javascript for retrieving IP Address info using ipinfo.io API via AJAX.
var GetIpInfo = function(ipAddr) {
var info = null;
var infoUrl = "http://ipinfo.io/" + ipAddr;
$.ajax({
url: infoUrl,
type: 'GET',
dataType: 'json',
async: false,
success: function(data) {
info = data;
}
});
return info;
};
// Convenience functions
var GetCoord = function(ipAddr) {
return GetIpInfo(ipAddr).loc;
};
var GetCity = function(ipAddr) {
return GetIpInfo(ipAddr).city;
};
var GetCountry = function(ipAddr) {
return GetIpInfo(ipAddr).country;
};
var GetHostname = function(ipAddr) {
return GetIpInfo(ipAddr).hostname;
};
var GetOrg = function(ipAddr) {
return GetIpInfo(ipAddr).org;
};
var GetPhone = function(ipAddr) {
return GetIpInfo(ipAddr).phone;
};
var GetPostal = function(ipAddr) {
return GetIpInfo(ipAddr).postal;
};
var GetRegion = function(ipAddr) {
return GetIpInfo(ipAddr).region;
};
@daveyx
Copy link

daveyx commented Mar 11, 2017

nice, but how to do it if i don't have the ipAddr?
i can call http://ipinfo.io/json and get a json, but i dont have any idea how to use it in javascript

@jhonteayudo
Copy link

nice, but how to do it if i don't have the ipAddr?
i can call http://ipinfo.io/json and get a json, but i dont have any idea how to use it in javascript

Do you resolve this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment