Getting Visitor’s Country in JavaScript

In some of the applications we may need to find the visitor’s location like City, Country,etc. Thereb is a very easy to use service provided by ws.geonames.org . The GeoNames geographical database covers all countries and contains over eight million placenames that are available for download free of charge.

Here is a Jquery Code which will give you the visitor’s Country and Country Code. You will need the updated version of jquery as well.

<script type="text/javascript">
if (navigator.geolocation) {
 navigator.geolocation.getCurrentPosition(function(position) {
 $.getJSON('http://ws.geonames.org/countryCode', {
 lat: position.coords.latitude,
 lng: position.coords.longitude,
 type: 'JSON'
 }, function(result) {
 alert('Country: ' + result.countryName + '\n' + 'Code: ' + result.countryCode);
 });
 });
 }
</script>