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.


