How do you detect a website visitor’s country?

WebTechRiser.com > Webmaster > How do you detect a website visitor’s country?

There are some free services out there on the Internet which let you find country and IP-based geolocalization from the client-side IP data.

wipmania is a free JSONP service that can be used to determine visitors’ countries. It’s really simple to use:

<script type="text/javascript">
  // plain JavaScript example
  function jsonpCallback(data) { 
    alert('Latitude: ' + data.latitude + 
          '\nLongitude: ' + data.longitude + 
          '\nCountry: ' + data.address.country); 
 }
</script>

<script src="http://api.wipmania.com/jsonp?callback=jsonpCallback" type="text/javascript"></script>

if you use a framework that supports JSONP, like jQuery, you can use:

// jQuery example
$.getJSON('http://api.wipmania.com/jsonp?callback=?',
  function (data) { 
  alert('Latitude: ' + data.latitude + 
        '\nLongitude: ' + data.longitude + 
        '\nCountry: ' + data.address.country); 
});

Check the above snippet running here.

Instead of an alert dialog (as in the above example), if you want to redirect a URL that takes a US visitor to a .COM address and allows a Canadian visitor passes through normally (for a .ca version of a website with a sister .com version site), simply use a “switch” statement in the “onload” event, check the “country” returned from the json function, then call Server.Transfer (asp.net) or similiar.

Also read:  How to combine all .vcf files to single .vcf file manually to merge into Gmail Contacts
Category Webmaster

Leave Your Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.