Guides
Last updated
January 24, 2021

How to do IP geolocation in jQuery

Emma Jagger
Emma Jagger

Table of Contents:

Get your free
IP Geolocation
 API key now
stars rating
4.8 from 1,863 votes
See why the best developers build on Abstract
START FOR FREE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required

Geolocating a visitor who reached your web pages directly from his browser is required when, for example, you need to display the prices of your products in his local currency, display a version of your content translated into his language, or display ads localized in his geographical area.

There are two main ways to geolocalize a visitor: browser-based geolocation and IP address-based geolocation.

How browser-based geolocation works

The navigator.geolocation javascript APIs of modern browsers make it possible to obtain the precise location. It is widely available and uses the device's GPS location, the GSM antennas' information, or information related to the network stack (IP address, MAC address).

It is easy to use in jQuery:



function get_location(location) {
var loc_info = "Your location details :\n";
loc_info += "Latitude: "+location.coords.latitude +"\n";
loc_info += "Longitude: "+location.coords.longitude+"\n";
console.log(loc_info);
}

if(navigator.geolocation)
navigator.geolocation.getCurrentPosition(get_location);

The major disadvantage of browser-based geolocation is that it will ask the user to share his location information with your scripts. This will certainly spoil the user experience because of an annoying pop-up. If the visitor refuses to share his location, then your script will not get any location information.

Also, you should note that geolocation API will only work over HTTPS. Hence you may have difficulties when testing it over your local development server.

How IP address geolocation works

An IP address identifies each device connected to the Internet. IANA is the body in charge of distributing these addresses between each continent and then passing responsibility to other organizations to distribute countries' addresses. Here is the list of those organizations:

  • AFRINIC for Africa Region
  • APNIC for Asia/Pacific Region
  • ARIN for Canada, USA, and some Caribbean Islands
  • LACNIC for Latin America and some Caribbean Islands
  • RIPE NCC for Europe, the Middle East, and Central Asia

Therefore, a geolocation service can use these organizations' data to find out in which country your visitors reside.

To obtain accurate location information at the level of regions, cities, and precise GPS coordinates, it will then be necessary to get databases from Internet service providers, as they are responsible for distributing IP addresses within a country.

The advantage of such a method is that it does not require any authorization from the visitor. The two disadvantages are that it is difficult and time-consuming to set up the IP address distribution database by oneself and keep it updated. Secondly, it is impossible to obtain the visitor's IP address directly in jQuery, just like in JavaScript, without an AJAX call to a server that can determine and return this address.

Enter an IP address to start
Need inspiration? Try
73.162.0.1
VALIDATE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Checking
5
Results for
ip address
Country:
TEST
Region:
TEST
City:
TEST
Coordinates:
TEST
Zip code:
TEST
Connection:
TEST
Get free credits, more data, and faster results

Geolocation in jQuery: using an API

The most practical solution is to use an API through an AJAX call to obtain the visitor's IP address and his geographical location.

Abstract provides the free IP Geolocation API that is fast and accurate. All you have to do is create an account, which does not require a credit card, to get your private API key. Here's how to use it in jQuery:



let api_key = "the_key_you_get_from_your_account";
$.getJSON("https://ipgeolocation.abstractapi.com/v1/?api_key=" + api_key, function(data) {
var loc_info = "Your location details :\n";
loc_info += "Latitude: "+data.latitude +"\n";
loc_info += "Longitude: "+data.longitude+"\n";
loc_info += "Timezone: GMT"+data.gmt_offset+"\n";
loc_info += "Country: "+data.country+"\n";
loc_info += "Region: "+data.region+"\n";
loc_info += "City: "+data.city+"\n";
console.log(loc_info);
})

Frequently Asked Questions

What is IP geolocation in jQuery?

IP geolocation in jQuery means making an AJAX call to a geolocation API that returns location data (such as country, region, city, latitude, and longitude) for the visitor's IP address. jQuery's $.getJSON method makes this straightforward: you pass your API endpoint and handle the JSON response in a callback. The result is location data available client-side without any browser permission prompt.

What is the difference between IP geolocation and browser geolocation in jQuery?

Browser geolocation uses the navigator.geolocation API to access device GPS or network data, but it requires the user to approve a permission popup and only works over HTTPS. IP geolocation uses a third-party API to look up the visitor's location from their IP address, no user permission is needed and it works in any context. IP geolocation is less precise but provides a much smoother user experience.

How do you call an IP geolocation API using jQuery?

Use jQuery's $.getJSON to send a GET request to the API endpoint with your API key as a query parameter. The callback receives the JSON response object, from which you can read fields like data.country, data.city, data.latitude, and data.longitude. For example, Abstract's IP Geolocation API endpoint is https://ipgeolocation.abstractapi.com/v1/?api_key=YOUR_KEY.

Can jQuery get a visitor's IP address directly without an API?

No. It is not possible to obtain a visitor's IP address using only client-side jQuery or JavaScript. An AJAX call to an external server or API is required, because only a server receiving the HTTP request can see the originating IP address. Third-party geolocation APIs handle this automatically and return both the IP and the associated location data in a single response.

When should you use IP geolocation in a jQuery project?

IP geolocation is a good fit when you need to show localized content, such as region-specific pricing, translated pages, or geo-targeted ads, without interrupting the user with a permission dialog. It is also useful when your app may not be served over HTTPS during development, since browser geolocation requires HTTPS while an IP geolocation API request does not have that restriction.

What location data does an IP geolocation API return in a jQuery app?

A typical IP geolocation API response includes fields such as country, region, city, latitude, longitude, timezone, and GMT offset. Some APIs also return ISP details, currency, and country calling code. In a jQuery callback you access these as properties on the response object, for example data.country, data.region, and data.gmt_offset.

Emma Jagger
Emma Jagger

Emma Jagger is an experienced engineer and Google alumna with a degree from Carnegie Mellon University. She specializes in email validation, IP geolocation, and API integration, focusing on creating practical and scalable solutions through her technical writing.

Get your free
IP Geolocation
key now
See why the best developers build on Abstract
get started for free

Related Articles

Get your free
IP Geolocation
key now
stars rating
4.8 from 1,863 votes
See why the best developers build on Abstract
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required