How to detect browser country in client site?
Solution 1
There are multiple options to determine the locale. In descending order of usefulness, these are:
- Look up the IP address, with an IP geolocation service like Maxmind GeoIP. This is the location the request is coming from, i.e. if an American vacations in Italy and uses a Swedish VPN, it will return
Sweden
.
It can only be done with the help of the server. The main advantage is that it's very reliable. The accuracy will be country or region for free services, city or region for paid ones.
- Look up the precise location on Earth from the browser with the geolocation API. An American vacationing in Italy using a Swedish VPN will register as Italy.
The answer will be very precise, usually no more than 10m off. In principle, it could work client-side, although you may want to perform the coordinate -> country lookup on the server. The main disadvantages are that not all devices have either GPS or WiFi position, and that it generally requires explicit user consent.
- Look in the
Accept-Language
header on the server (or with the help of the server), and extract the locale information. An American vacationing in Italy using a Swedish VPN will register as USA.
The downside is that this is a setting that's extremely easy to change. For instance, English speakers around the world may prefer en-US
settings in order to avoid machine-translated text. On modern browsers (as of writing not IE/Edge, and only Safari 11+), you can also request navigator.languages.
-
navigator.language
is the first element of thenavigator.languages
header. All of the considerations of navigator.languages apply. On top, this information can sometimes be just the language without any locale (i.e.en
instead ofen-US
). -
Use another, third-party service. For instance, if the user signs in via a Single-Sign-On system such Facebook connect, you can request the hometown of the user. This information is typically very unreliable, and requires a third party.
Solution 2
Another option could be using the (internationalization API)
console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)
Related videos on Youtube
![KevinHu](https://lh3.googleusercontent.com/-saGkkSg8vEo/AAAAAAAAAAI/AAAAAAAAABE/KeA_cIJYYfQ/photo.jpg?sz=256)
KevinHu
Hello, I'm looking for a abroad job now. Following is my info, please give me a change to have an interview, thanks. profile website : http://www.sky790312.tk github : https://github.com/sky790312 I'm a creative Frontend developer with about 3 years of professional experience, based in Taiwan now. I develop modern, interactive, responsive websites. keep in touch to cutting edge technologies such as AngularJS, React and other tools. My BackEnd background allows me to take care of a project through to execution and delivery.
Updated on July 09, 2022Comments
-
KevinHu almost 2 years
I found the following website can detect my country and currency even in the private mode
http://www.innisfreeworld.com/
It records in cookie, How do it do that?
Is that possible fulfill in client site ? navigator?
-
symcbean over 6 yearsProbably using geoIP lookup. This runs serverside and is very different from browser geolocation.
-
Lalit over 6 years
-
Michael C. over 6 yearsIt should be using IP geolocation technology. You can refer to geolocation.com for explanation.
-
-
KevinHu over 6 yearsSo..there is no way to detect on the client side without asking user permission (geolocation api), right?
-
phihag over 6 yearsNo, option 3 works on newer clients, and 4 on all clients. Option 5 may work without user interaction, by collaborating with a third-party service such as google analytics. None of those require asking the user for permission.
-
MarsAndBack over 5 yearsAs well, most geoip services need you to use an API key. Doing this in the front-end exposes your API key... someone could use it and abuse it.
-
dinvlad over 3 yearsGeoIP doesn't ask for permission either, correct?
-
phihag over 3 years@dinvlad Correct, GeoIP is entirely server-side, the browser doesn't even notice it.
-
Aamnah almost 3 yearsmay not work accurately because one timezone can be in multiple countries. For example,
Europe/Zurich
is in Germany, Switzerland and Liechtenstein.. -
Owen almost 3 years@Aamnah there are actually separate timezone definitions for each country (for example
Europe/Berlin
andEurope/Vaduz
), that the user has probably set correctly. But there's definitely no guarantee. -
John about 2 yearsThis is an excellent answer. Every country has at least one dedicated time zone name. Using languages instead is a catastrophe because English is used around the world. I have my PC set to English and I'm in Germany. IP-based stuff works well but this solution here is easy and cheap.