The other day I got a question asked, about geolocation.
How would I show a different phone number for my company’s website, based on the current user location?
There are many different ways to solve this problem.
Let’s analyze few of them:
1) We can use the browser language settings, detected via JavaScript and then display a different phone number based on the user’s nation.
2) We could instead download one of the free ip to nation database mappings that there are on the net (or use one of the ip2country services), and code some server side script that will determine the user location.
3) Use Google JS API (that is a sort of ip2country webservice anyway…) to determine the user location, plus JavaScript.
I would say that number 1 is definitely too weak.
Number 2, if I have to maintain my own ip/country database it is going to be a nightmare over time, and I don’t think there are companies (or at least I’m not aware of them) big and reliable enough that can offer a proper ip2country free webservice.
I would definitely go for number 3 and as personal choice I would use jQuery as JavaScript library.
To gain further flexibility I would also add a dropdown where the default country value is the one that Google chooses as user’s position (if determined at all), and the user can interact with the dropdown if necessary, displaying a different phone number/country details.
The sample JavaScript/HTML code to be able to achieve this functionality is attached here (right click and save as).
NB: It is required a local copy of jQuery (saved as jquery.js)