Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How to find the operating system version using JavaScript

like image 438
David John Avatar asked Apr 29 '17 01:04

David John


People also ask

How do you call a function named myFunction?

Define a function named "myFunction", and make it display "Hello World!" in the <p> element. Hint: Use the function keyword to define the function (followed by a name, followed by parentheses). Place the code you want executed by the function, inside curly brackets. Then, call the function.

What are the different types of functions in JavaScript?

There are 3 ways of writing a function in JavaScript: Function Declaration. Function Expression. Arrow Function.

How do you determine whether a function exists by using the type of operator?

In this example, we use the '===' operator. This operator, called the Strict Equal operator, checks if the operands are of the same type. Example: This example uses === operator to check a variable is of function type or not. Using object.


1 Answers

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.

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

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

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

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.

like image 139
Navneet Singh Avatar answered Nov 01 '22 13:11

Navneet Singh