` HTML 元素`
The HTML Element

原始链接: https://developer.chrome.com/blog/geolocation-html-element

## 新的 <geolocation> HTML 元素改进了位置访问 从 Chrome 144 开始,开发者可以使用新的 <geolocation> HTML 元素来请求用户位置。这标志着从 JavaScript 触发的权限提示转变为更以用户为中心、声明式的方法。广泛的测试,包括与 Zoom、Immobiliare.it 和 ZapImóveis 的源试用,显示出显著的改进——位置错误减少高达 46.9%,先前被阻止的权限恢复率达到 54.4%。 <geolocation> 元素简化了代码,减少了浏览器干预(例如静默阻止重复提示),并提供了更清晰的用户意图。它从通用的 <permission> 元素概念演变为一个专门的控件,用于摄像头/麦克风访问的 <usermedia> 元素也在开发中。 与传统的 Geolocation API 不同,该元素充当数据中介,由用户点击触发,提供自动刷新和通过 `onlocation` 事件简化错误处理。样式受到限制,以确保清晰并防止欺骗性做法。开发者可以实现渐进增强,为不支持的浏览器提供回退,甚至可以使用 polyfill 以获得更广泛的兼容性。

Hacker News 上正在讨论 Chrome 新的 `` HTML 元素。文章链接到 Chrome 开发者博客,详细介绍了该功能,旨在改进网站请求和处理地理位置权限的方式。 用户们争论了为 polyfill 示例选择 `unpkg.com` 的决定,一位评论者提倡使用更可靠的 `jsdelivr.com`。 另一位评论者提供了背景信息,解释说该元素是更大努力的一部分,旨在创建更流畅、更用户友好的浏览器权限处理机制——当前系统被认为是一个“设计陷阱”。 另一条评论批评了 Chrome 的开发流程,认为存在一种模式,即在缺乏广泛厂商共识的情况下发布功能。 一条回复质疑了这一说法,指出博客文章中声称已经纳入了反馈意见。 此次讨论凸显了新元素带来的潜在好处以及对标准化实践的担忧。
相关文章

原文

Published: January 13, 2026

From Chrome 144 you can use the new <geolocation> HTML element. This element represents a major shift in how sites request user location data—moving away from script-triggered permission prompts toward a declarative, user-action-oriented experience. It reduces the boilerplate code required to handle permission states and errors, and provides a stronger signal of user intent, which helps avoid browser interventions (like quiet blocks).

This launch is the result of extensive real-world testing and rigorous discussion with the web standards community. To understand the utility of this element, it is important to examine the history of its development and the data that drove its design.

From generic <permission> to specific <geolocation>

The <geolocation> element is the latest evolution of the Page-Embedded Permission Control initiative, where it initially was proposed as a generic <permission> element with a type attribute (see the original explainer). The value of the type attribute (for example, "geolocation") would determine the type of the requested permission. For example, the initial proposal includes values such as camera, microphone, and geolocation.

Validation of the concept

We ran an origin trial for the generic <permission> element from Chrome 126 to 143. The aim of this trial was to test the hypothesis that a dedicated, in-context button would improve user trust and decision-making.

The results from this origin trial supported the validation of this core concept:

  • Zoom reported a 46.9% decrease in camera or microphone capture errors (such as system-level blockers) by using the element to guide users through recovery.
  • Immobiliare.it saw a 20% increase in successful geolocation flows.
  • ZapImóveis observed a 54.4% success rate in users recovering from a "previously blocked" state when presented with the element.

Redefinition of the design

While the concept proved successful, the implementation required refinement. Feedback from browser vendors—including Apple (Safari/WebKit) and Mozilla (Firefox)—indicated that a "one-size-fits-all" element introduced significant complexity regarding unique capability behaviors.

Consequently, we transitioned from a generic permission control to targeted, capability-specific elements (see WICG discussion). The <geolocation> element is the first of these specialized controls to launch. Following this, we're also developing a dedicated <usermedia> element (for camera and microphone access), which has its own separate origin trial.

Unlike the original proposal, which focused on managing permission state (that is, allow or deny), these new elements function as data mediators, effectively replacing the need to call the JavaScript APIs directly for most use cases.

This table describes the differences between the Geolocation JavaScript API, the <permission> element and the new <geolocation> element.
Feature Geolocation JS API <permission> HTML Element <geolocation> HTML Element
Triggering Event for Permission Prompt Imperative script execution (getCurrentPosition) User clicks on the browser-controlled <permission> element User clicks on the browser-controlled <geolocation> element
Browser Role Decides prompt based on state Acts as a permission mediator Acts as a data mediator
Site Responsibility Manually call the JavaScript API, handle callbacks & manage permission errors Implement geolocation API once permission has been granted Listen to location event
Core Goal Basic location access Permission request Permission request and location access

Why use the <geolocation> element?

Currently, geolocation flows rely on the Geolocation API, which triggers permission prompts that can interrupt users if fired out of context or even on page load. Crucially, reliance on these imperative prompts is becoming less viable due to browser interventions. For example, Chrome actively blocks permission requests if a user has dismissed the prompt three times, enforcing a temporary quiet block that initially lasts for one week. This means legacy code attempting to trigger a prompt may silently fail, leaving the user with a broken experience and no clear way to enable the feature. Furthermore, standard prompts often lack context. If a prompt appears unexpectedly, users may block it reflexively or accidentally, unaware that this decision creates a permanent block that is difficult to reverse. This context gap—rather than the feature itself—is a primary driver of high denial rates.

The <geolocation> element resolves the context gap problem by ensuring requests are strictly user-initiated. This model provides three distinct advantages:

  • Clear intent and timing: By clicking a use location button, the user explicitly signals their intent to use their location at that specific moment. This signals that they understand the value and actively want to use location, turning a potential block into a successful interaction.
  • Simplified recovery: If a user previously blocked location access when browsing a site (perhaps by accident or lack of context), clicking the element triggers a specialized recovery flow. This helps them re-enable location at the moment when they actually want to use location, without the friction of navigating deep into the browser's site settings.
  • Automatic refresh: If permission is already granted, clicking the element acts as a refresh button, fetching new data immediately without re-prompting.

Implementation

Integrating the element requires significantly less boilerplate than the JavaScript API. Instead of managing callbacks and error states manually, developers can add the tag to the page and listen for the onlocation event.

<geolocation
  onlocation="handleLocation(event)"
  autolocate
  accuracymode="precise">
</geolocation>
function handleLocation(event) {
  // Directly access the GeolocationPosition object on the element
  if (event.target.position) {
    const { latitude, longitude } = event.target.position.coords;
    console.log("Location retrieved:", latitude, longitude);
  } else if (event.target.error) {
    console.error("Error:", event.target.error.message);
  }
}

Key attributes and properties

  • autolocate: Automatically attempt to retrieve location when the element loads, but only if the current permission status already allows it (preventing unexpected prompts).
  • accuracymode: Accepts a value of "precise" or "approximate", corresponding to the standard enableHighAccuracy option.
  • watch: Switches behavior to match watchPosition(), firing events continuously as the user moves.
  • position: A read-only property on the DOM element returning the GeolocationPosition object once available.
  • error: A read-only property returning a GeolocationPositionError if the request fails.

Styling constraints

To ensure user trust and prevent deceptive design patterns, the <geolocation> element applies specific styling restrictions similar to the earlier <permission> element experiment. While you can customize the button to match their site's theme, the browser enforces several guardrails:

  • Legibility: Text and background colors are checked for sufficient contrast (typically a ratio of at least 3:1) to ensure the permission request is always readable. Additionally, the alpha channel (opacity) must be set to 1 to prevent the element from being deceptively transparent.
  • Sizing and spacing: The element enforces minimum and maximum bounds for width, height, and font size. Negative margins or outline offsets are disabled to prevent the element from being visually obscured or overlapping other content deceptively.
  • Visual integrity: Distorting effects are limited—for example, transform supports only 2D translations and proportional scaling.
  • CSS pseudo-classes: The element supports state-based styling, such as :granted (when permission is active).

Progressive enhancement strategy

We understand that standardizing new HTML elements is a gradual process. However, developers can adopt the <geolocation> element today without breaking compatibility for users on other browsers.

The element is designed to degrade gracefully. Browsers that don't support the <geolocation> element will treat it as an HTMLUnknownElement. Importantly, if the browser supports the element, it won't render the children. This allows writing the HTML cleanly for both supported and unsupported browsers.

Custom fallback pattern

If you want to fully control the fallback experience yourself, you can make use of child elements like a button that you hook up with the regular JavaScript Geolocation API.

<geolocation onlocation="updateMap()">
  <!-- Fallback contents if the element is not supported -->
  <button onclick="navigator.geolocation.getCurrentPosition(updateMap)">
    Use my location
  </button>
</geolocation>

Demo

The demo linked in the caption with buttons to test the three configuration types.
The <geolocation> element demo showing the three main configurations: Manual Request, Automatic Request (using autolocate), and Watch Location (using watch). You can test these behaviors on the live demo page.

Polyfill

You can alternatively install a polyfill from npm that transparently and automatically replaces all occurrences of <geolocation> with a custom element <geo-location> (note the dash) backed by the regular JavaScript Geolocation API. If the browser supports the <geolocation> element, the polyfill simply does nothing. Check out this polyfill demo that shows the polyfill in action. The source code is on GitHub.

if (!('HTMLGeolocationElement' in window)) {
  await import('https://unpkg.com/geolocation-element-polyfill/index.js');
}
<geolocation onlocation="updateMap()"></geolocation>

Feature detection

For more complex logic, you can programmatically detect support using the interface:

if ('HTMLGeolocationElement' in window) {
  // Use modern <geolocation> element logic
} else {
  // Fallback to legacy navigator.geolocation API
}

Wrap up

We're excited to see how developers will implement more performant location retrial scenarios by using the new <geolocation> HTML element. It represents a shift toward capability-specific elements that are tailored to how users actually use the web today.

For other permission use cases, from Chrome 144 you can join the <usermedia> HTML element origin trial, bringing these same ergonomic benefits to camera and microphone.

Acknowledgements

This document was reviewed by Andy Paicu, Gilberto Cocchi, and Rachel Andrew.

联系我们 contact @ memedata.com