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 . 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('', {
 lat: position.coords.latitude,
 lng: position.coords.longitude,
 type: 'JSON'
 }, function(result) {
 alert('Country: ' + result.countryName + '\n' + 'Code: ' + result.countryCode);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s