Get the location from an IP Address in Javascript

How to get the location from an IP Address in Javascript and JQuery.

One of ipdata's most useful features is that if you use it client-side with Javascript you don't have to pass in the user's IP address. We will automatically get the user's IP Address, geolocate it and return the geolocation data to you client-side.

An example request in JQuery

$.get("https://ipforensics.net/api/v1/origin?apikey=APIKEY", function(response) {
    console.log(response.meta_data.geolocation);
}, "jsonp");

A simple call like above without passing in any parameters other than the API key will give you the location of the user currently on the page with the above code embedded.

Pure Javascript

var request = new XMLHttpRequest();

request.open('GET', 'https://ipforensics.net/api/v1/origin?apikey=APIKEY');

request.setRequestHeader('Accept', 'application/json');

request.onreadystatechange = function () {
  if (this.readyState === 4) {
    console.log(this.responseText);
  }
};

request.send();

You can pretty much used any programming language of choice to get user location by just making a get HTTP request to IpForensics endpoint.

Always reference the API endpoint for complete response result

Last updated