MDN self-hosted library · e01.org

HTMLGeolocationElement: watch property

Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.

html
<geolocation watch></geolocation>
js
const geo = document.querySelector("geolocation");
console.log(geo.watch); // true
html
<geolocation watch></geolocation>
<p id="output"></p>
js
const outputElem = document.querySelector("#output");
const geo = document.querySelector("geolocation");
console.log(geo.watch); // true
js
geo.addEventListener("location", () => {
  if (geo.position) {
    outputElem.textContent += `(${geo.position.coords.latitude},${geo.position.coords.longitude}), `;
  } else if (geo.error) {
    outputElem.textContent += `${geo.error.message}, `;
  }
});