Categories
JavaScript Answers

How to get visitor’s location using geolocation with JavaScript?

Spread the love

To get visitor’s location using geolocation with JavaScript, we can use the Ipregistry API.

For instance, we write

const response = await fetch("https://api.ipregistry.co/?key=tryout");
const payload = await response.json();
console.log(payload.location.country.name, payload.location.city);

to call fetch to make a get request to https://api.ipregistry.co/?key=tryout

Then we get the response from it with json.

And then we get the country and city data from the payload.

We put that in an async function.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.