HTML geolocation means reading the device’s location from a webpage so you can show nearby results, place a marker on a map, or personalize content. The browser asks the user for permission first. Access goes through navigator.geolocation (the Geolocation API).
How does an HTML page use it?
Typically a <button> starts the request. A script calls getCurrentPosition or watchPosition, then writes latitude/longitude into a <p> or other element. Never grab location silently on page load without a clear user action.
What about the HTML <geolocation> element?
MDN also documents an experimental<geolocation> element: a browser-defined control with built-in UI and a clearer permission flow. It still uses the Geolocation API underneath. Support is limited today—nest a fallback <button> that calls the API for other browsers.
When should you use geolocation on a site?
Store finders, weather for “near me,” delivery ETA, or check-in features. Always explain why you need location and provide a manual address field if the user declines.
Secure context required.
Modern browsers expose geolocation on HTTPS pages (and usually localhost for development).
Privacy first.
Location can identify a person. Ask only when needed, use the data for the stated purpose, and do not store it longer than necessary.
iframes need permission.
Third-party frames may need Permissions-Policy: geolocation=… and allow="geolocation" on the <iframe>.
Syntax
HTML / Script Syntax
Classic pattern: button in HTML, Geolocation API in a script:
HTML provides the control and a place for results.
Feature detect before calling the API.
Success fills the paragraph; error shows the reason.
Same UX goal as <geolocation> with a nested fallback button on unsupported browsers.
Edge Cases & Pitfalls
Most failures are permission, insecure context, or missing error UI—not missing HTML tags.
https
HTTP (non-secure) pages
Geolocation is blocked outside secure contexts. Deploy HTTPS for production HTML.
deny
User denies permission
Handle code 1 and offer a city/ZIP form. Do not spam the prompt.
watch
Leaving watchPosition running
Call clearWatch when the user stops tracking or navigates away.
iframe
Embedded frames
May need Permissions-Policy and allow="geolocation" on the iframe.
geo-el
Relying only on <geolocation>
Limited availability. Always nest a button + API fallback for today’s browsers.
china
Regional availability
Some regions may need local providers instead of the vanilla browser service.
Compatibility
Browser Support
navigator.geolocation is widely available in modern browsers (secure contexts). The HTML geolocation element is experimental with limited availability.
✓ API: Baseline · Element: Limited
Geolocation on HTML pages
Use the Geolocation API from page scripts everywhere users browse securely; treatas progressive enhancement.
APInavigator.geolocation
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
Geolocation APIWide
Bottom line: Ship getCurrentPosition behind a button today; addwith a nested fallback when you want declarative UI later.
Remember
Key Takeaways
navigator.geolocation powers location in HTML pages (HTTPS + permission).
Button + script is the reliable pattern; show coords in the DOM.
getCurrentPosition once; watchPosition for live updates.
<geolocation> is experimental—use a nested button fallback.
One line: User activates a control → browser asks permission → script reads coords → HTML shows the result.
Frequently Asked Questions
It means reading the device’s location from a webpage. The Geolocation API is reached via navigator.geolocation. A page script asks for permission, then getCurrentPosition or watchPosition returns coordinates you can show in HTML.
The experimental <geolocation> element is a browser-defined control for sharing location with clearer UI and permission flow. It uses the Geolocation API under the hood. Support is limited—include a <button> fallback that calls getCurrentPosition.
Yes for real sites. Geolocation is a secure-context feature in modern browsers. Localhost is usually allowed for learning; production pages should be served over HTTPS.
getCurrentPosition fetches one fix. watchPosition calls your success handler whenever the position updates until you clearWatch. Use watch for live tracking; always clearWatch when done.
Location is sensitive. Browsers prompt for permission when you request it. Triggering from a clear user gesture (button click) matches user expectations and pairs well with the upcoming geolocation element’s conscious-choice model.
PERMISSION_DENIED (1), POSITION_UNAVAILABLE (2), and TIMEOUT (3). Show a friendly message in the page and offer a manual address fallback when permission is denied.