javascript 如何在客户端站点中检测浏览器国家?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48278024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:56:21  来源:igfitidea点击:

How to detect browser country in client site?

javascripthtmlbrowsernavigator

提问by KevinHu

I found the following website can detect my country and currency even in the private mode

我发现以下网站即使在私人模式下也能检测到我的国家和货币

http://www.innisfreeworld.com/

http://www.innisfreeworld.com/

It records in cookie, How do it do that?

它记录在 cookie 中,它是如何做到的?

enter image description here

enter image description here

Is that possible fulfill in client site ? navigator?

这可能在客户网站上实现吗?航海家?

回答by phihag

There are multiple options to determine the locale. In descending order of usefulness, these are:

有多种选项可以确定语言环境。按有用性的降序排列,它们是:

  1. Look up the IP address, with an IP geolocation servicelike 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.
  1. 中查找IP地址一个IP地理定位服务一样的MaxMind的GeoIP。这是请求来自的位置,即如果美国人在意大利度假并使用瑞典 VPN,它将返回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.

它只能在服务器的帮助下完成。主要优点是它非常可靠。免费服务准确度为国家或地区,付费服务准确度为城市或地区。

  1. 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.
  1. 使用geolocation API从浏览器中查找地球上的精确位置。使用瑞典 VPN 在意大利度假的美国人将注册为意大利。

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.

答案会非常精确,通常不会超过 10m。原则上,它可以在客户端工作,尽管您可能希望在服务器上执行坐标 -> 国家/地区查找。主要缺点是并非所有设备都具有 GPS 或 WiFi 位置,并且通常需要明确的用户同意。

  1. Look in the Accept-Languageheader 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.
  1. 查看Accept-Language服务器上的标题(或在服务器的帮助下),并提取区域设置信息。使用瑞典 VPN 在意大利度假的美国人将注册为美国。

The downside is that this is a setting that's extremely easy to change. For instance, English speakers around the world may prefer en-USsettings 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.

缺点是这是一个非常容易更改的设置。例如,世界各地的英语使用者可能更喜欢en-US设置以避免机器翻译的文本。在现代浏览器上(截至编写时不是 IE/Edge,只有 Safari 11+),您还可以请求navigator.languages

  1. navigator.languageis the first element of the navigator.languagesheader. All of the considerations of navigator.languages apply. On top, this information can sometimes be just the language without any locale (i.e. eninstead of en-US).

  2. 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.

  1. navigator.languagenavigator.languages标题的第一个元素。navigator.languages 的所有注意事项都适用。最重要的是,这些信息有时可能只是没有任何语言环境的语言(即en代替en-US)。

  2. 使用另一个第三方服务。例如,如果用户通过单点登录系统(如 Facebook 连接)登录,您可以请求用户的家乡。此信息通常非常不可靠,并且需要第三方。