简体   繁体   English

如何只在一个国家和操作系统中运行javascript函数?

[英]How to run javascript function only in one Country and OS?

I have seen similar questions like below. 我见过类似的问题如下。 But none of them works for me. 但它们都不适合我。 Please help. 请帮忙。 Find the three letter country code using HTML and Javascript 使用HTML和Javascript查找三个字母的国家/地区代码

How to find the operating system version using JavaScript 如何使用JavaScript查找操作系统版本

There were some security features adopted by the browser to stop JavaScript getting the user's location without their consent. 浏览器采用了一些安全功能来阻止JavaScript在未经用户同意的情况下获取用户的位置。 Now, you can use the following code to get the user's location. 现在,您可以使用以下代码获取用户的位置。 It will open a prompt window for the user to allow getting his/her location. 它将打开一个提示窗口,供用户允许获取他/她的位置。

var recieveLocation = function(pos) {
    console.log(pos); // Latitude and Longitude Info 
}

navigator.geolocation.getCurrentPosition(recieveLocation);

The code above will return the user's geolocation. 上面的代码将返回用户的地理位置。 You will need to decode those longitude and latitude using the third party service to get the country information. 您需要使用第三方服务解码这些经度和纬度以获取国家/地区信息。

For an example, check Google Map Geocoding services API: 例如,请查看Google Map Geocoding服务API:
https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple

Note: Most web browsers will need HTTPS to run the code. 注意:大多数Web浏览器都需要HTTPS才能运行代码。 Check the console for the error message. 检查控制台是否有错误消息。 For Mozilla Firefox, it will say: 对于Mozilla Firefox,它会说:

[Deprecation] getCurrentPosition() and watchPosition() no longer work on insecure origins. [Deprecation] getCurrentPosition()和watchPosition()不再适用于不安全的起源。 To use this feature, you should consider switching your application to a secure origin, such as HTTPS. 要使用此功能,您应该考虑将应用程序切换到安全的来源,例如HTTPS。

You will need to install the SSL certificate on the domain so the browser can trust on the website to allow getting the user's geolocation. 您需要在域上安装SSL证书,以便浏览器可以信任该网站以允许获取用户的地理位置。

navigator.language string contains a "county code". navigator.language字符串包含“县代码”。

navigator.platform string contains operating system and computer processor type. navigator.platform字符串包含操作系统和计算机处理器类型。

Both can be spoofed. 两者都可以被欺骗。

An alternative approach would be to utilize html form to ask user what "country" they are currently reside in or identify with. 另一种方法是利用html form询问用户他们当前居住或与之相关的“国家/地区”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM