2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

How to get the user country name from Clint side using JQuery.

I am working on a requirement where we need to find the country name for the registration user without any burden giving to the user to select the country name manually. That"s why am looking for a solution to get the user country name from client side by using javascript or jquery. First of all I want to confirm that, is the requirement valid or not. And if yes then how we can track the user country name automatically without any user interruption.
userimage

Yes, its possible. We can able track the user country name from the client side using JQuery by invoking  the 3rd party URL through Ajax call.


Please find the example here,


<script language="javascript" type="text/javascript" src="https://code.jquery.com/jquery-3.0.0.min.js"></script>

<script>

    $(document).ready(function(){

        $.getJSON("http://freegeoip.net/json/", function(data) {

            var country = data.country_name;

            alert("country " + country);

        });

    });

</script>


The request url will give the below result. Now its depends on your choice whatever the data need from the user at the time of registration.


{"ip":"xxx","country_code":"IN","country_name":"India","region_code":"","region_name":"","city":"","zip_code":"","time_zone":"Asia/Kolkata","latitude":20,"longitude":77,"metro_code":0}


Answer is