Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here



Google

How to get clients location from its IP Address using jQuery JavaScript


Share with WhatsApp


This post contains information or you can say a quick how to guide for “How to get country name or location of site’s visitor by IP address in jQuery JavaScript”.

In one of my project it was a requirement that check IP address of the visitor and according to its country redirect him to different version of the same site so while working on that I came across several API’s by which you can get the location just by single jQuery ajax call or just by including a lightweight javascript.

Here I am sharing about some of those free Geo Location API’s with live sample. Make sure that you have included jQuery library reference as all below sample code is of jQuery.

Ip.CodeHelper.io

It’s a free Geo Location API Supporting JSON with JS Callback, PHP and other languages like .Net, C++, Java with SSL etc.

Just you need to include their JavaScript file (Approx. size 872 bytes) and you can get all information about clients location such as its country code, country name, city name and many more.

Below is the sample code.

<script type="text/javascript" src="http://www.codehelper.io/api/ips/?js"></script>
<script type="text/javascript">
        alert('Your country code is ' + codehelper_ip.Country);   
</script>

FreeGeoIp.Net

It’s a public REST API for searching geo location of IP addresses and host names. It has an internal database with geolocation information, which is queried via the API.

Just you need to send HTTP GET request with an IP as an optional parameter. It supports multiple formats such as csv, xml and json.

API usage is limited to 10,000 queries per hour. After reaching this limit, all requests will result in HTTP 403 (Forbidden) until the roll over.

If the usage limit is a problem, you can consider running your own instance of this system. It’s open source and freely available at GitHub.

Below is sample code.

<script type="text/javascript">
        $.getJSON('http://freegeoip.net/json/', function (location) {
         alert('Your country code is ' + location.country_code);
        });    
</script>

Hostip.info API

Hostip.info is an open project i.e.  a community-based project to find geolocation by IP address. Database is free to use but they have mentioned on their home page that though this database provides accurate location still if you want more accurate location tracking for commercial project then you can consider using “MaxMind”. The Hostip.info operations (hosting and bandwidth costs, etc) are sponsored by Net Industries, LLC.

Below is the sample code.

<script type="text/javascript">
      $.get("http://api.hostip.info/country.php", function (data) {
            alert('Your country code is ' + data);
        });
</script>

I have tested all above API toghether and found CodeHelper’s response is fast most of the times and below is the full code.

<!DOCTYPE html>
<html>
<head>
    <title>ImInfo.in - Tutorial to get geo location by IP in jquery/javascript</title>
</head>
<body>
    <div class="result">
    </div>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <!--Using freegeoip.net API-->
    <script type="text/javascript">
        $.getJSON('http://freegeoip.net/json/', function (location) {
            $(".result").append('<br>Using freegeoip.net : Your country code is ' + location.country_code + '<br>');
        });    
    </script>
    <!--Using hostip.info API-->
    <script type="text/javascript">
        $.get("http://api.hostip.info/country.php", function (data) {
            $(".result").append('<br>Using hostip.info : Your country code is ' + data + '<br>');
        });
    </script>
    <!--Using codehelper.io API-->
    <script type="text/javascript" src="http://www.codehelper.io/api/ips/?js"></script>
    <script type="text/javascript">
        $(".result").append('<br>Using CodeHelper : Your country code is ' + codehelper_ip.Country + '<br>');   
    </script>
</body>
</html>

Let me know which is better in your opinion in comment section below. Thanks.



If you enjoyed this post take 5 seconds to share it! Be Socialable. :-)

Share with WhatsApp

Posts To Read Next

Typewriter - Automatic TypeScript Template generation from C# classes in Visual Studio

Typewriter, an extension for visual studio that generates TypeScript files from C# code files using TypeScript templates. Convert your C# model classes to TypeScript automatically.


Swagger - Powerful Opensource Framework for RESTful API

With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability. Swagger Editor allows you to generate code, documentation with ease.


Visual Studio Code - Lighweight but powerful source code editor available for Windows, OS X and Linux

Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, OS X and Linux. It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages (C++, C#, Python, PHP) and runtimes.


Assembla - Free and private repository to manage your source code online with SVN subversion hosting

With Assembla you can share source code with others online. Free & Private source code repository with SVN Subversion, Git & Perforce Hosting.


Top 10 Visual Studio things which can boost developers coding speed

Visual Studio 2012 provides some coding features by which you can code faster if use them properly. This post will cover top 10 things among them to boost your development speed.


Your opinion is valuable for us! Comments, suggetions are welcome.


Submit your Email Id to stay updated with us and get notified with our new posts. It's FREE!
Vu 32'' TV Vu 24'' TV
We know this popup is disturbing you!
But We would greatly appreciate if you share us with your friends below!

It will not take more than 2 seconds but will motivate us greatly to write more,share more!

x