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

时间:2022-03-17 06:50:20

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查找操作系统版本

2 个解决方案

#1


7  

There were some security features adopted by the browser to stop JavaScript getting the user's location without their consent. 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.

浏览器采用了一些安全功能来阻止JavaScript在未经用户同意的情况下获取用户的位置。现在,您可以使用以下代码获取用户的位置。它将打开一个提示窗口,供用户允许获取他/她的位置。

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:
https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple

例如,请查看Google地图地理编码服务API:https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple

Note: Most web browsers will need HTTPS to run the code. Check the console for the error message. For Mozilla Firefox, it will say:

注意:大多数Web浏览器都需要HTTPS才能运行代码。检查控制台是否有错误消息。对于Mozilla Firefox,它会说:

[Deprecation] getCurrentPosition() and watchPosition() no longer work on insecure origins. To use this feature, you should consider switching your application to a secure origin, such as HTTPS.

[Deprecation] getCurrentPosition()和watchPosition()不再适用于不安全的起源。要使用此功能,您应该考虑将应用程序切换到安全的来源,例如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证书,以便浏览器可以信任该网站以允许获取用户的地理位置。

#2


0  

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表单询问用户他们当前居住或识别的“国家”。

#1


7  

There were some security features adopted by the browser to stop JavaScript getting the user's location without their consent. 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.

浏览器采用了一些安全功能来阻止JavaScript在未经用户同意的情况下获取用户的位置。现在,您可以使用以下代码获取用户的位置。它将打开一个提示窗口,供用户允许获取他/她的位置。

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:
https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple

例如,请查看Google地图地理编码服务API:https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple

Note: Most web browsers will need HTTPS to run the code. Check the console for the error message. For Mozilla Firefox, it will say:

注意:大多数Web浏览器都需要HTTPS才能运行代码。检查控制台是否有错误消息。对于Mozilla Firefox,它会说:

[Deprecation] getCurrentPosition() and watchPosition() no longer work on insecure origins. To use this feature, you should consider switching your application to a secure origin, such as HTTPS.

[Deprecation] getCurrentPosition()和watchPosition()不再适用于不安全的起源。要使用此功能,您应该考虑将应用程序切换到安全的来源,例如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证书,以便浏览器可以信任该网站以允许获取用户的地理位置。

#2


0  

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表单询问用户他们当前居住或识别的“国家”。