V
V
Vladislav2021-06-22 10:32:03
JavaScript
Vladislav, 2021-06-22 10:32:03

How to determine the user's city in pure JavaScript?

How to determine the user's city in pure JavaScript, otherwise there are JQuery options everywhere, and please, without stupid comments, only to the point if you know, but keep your opinion to yourself

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladislav, 2021-06-22
@SlavaMaxwell

Found it, maybe it will help someone

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://api-maps.yandex.ru/2.0-stable/?load=package.standard&lang=ru-RU" type="text/javascript"></script>

</head>
<body>
        <div id="city"></div> 
<div id="region"></div> 
<div id="country"></div>


</body>
</html>

window.onload = function () {
  const o = ymaps.geolocation;
  document.getElementById("city").innerHTML = o.city;
  document.getElementById("region").innerHTML = o.region;
  document.getElementById("country").innerHTML = o.country;
}

H
Hecktosaurus, 2021-06-22
@Hecktosaurus

Request to ipinfo.io with result caching to avoid exceeding limits
Or native geolocation of the browser if the user gives the go-ahead

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question