大网数据广告活动
Web 标准与前端开发

HTMLGeolocationElement: permissionStatus property

· 52CE 我爱测

The **`permissionStatus`** read-only property of the HTMLGeolocationElement interface returns an enumerated value representing the current permission status for the `geolocation` feature.

Navigation API

The permissionStatus read-only property of the HTMLGeolocationElement interface returns an enumerated value representing the current permission status for the geolocation feature.

If you want to access the initial permission status for the geolocation feature as it was when the page first loaded, use the HTMLGeolocationElement.initialPermissionStatus property.

Value

An enumerated value, which can be one of:

  • granted
    • : The user has granted permission for the browser to use the geolocation feature, either via the geolocation element or some other mechanism. When using the <geolocation> element, this means that the user has pressed the rendered button and selected an "allow" option, at which point the browser will start to request location data.
  • denied
    • : The user has denied permission for the browser to use the geolocation feature, either via the <geolocation> element or some other mechanism. When using the <geolocation> element, this means that the user has pressed the rendered button and selected a "don't allow" option, at which point the browser will not request location data until the user presses the rendered button again and selects an "allow" option.
  • prompt
    • : The user has not specifically granted or denied permission for the browser to use the geolocation feature, which means that the browser won't request location data until they grant permission. When using the <geolocation> element, this means that the user has not yet pressed the rendered button. When they do, they'll be given the option to grant or deny permission for the browser to request location data.

The permission status persists between page loads. If the <geolocation> element has its autolocate attribute set to true, and permission was previously granted, the browser will start to request location data as soon as the <geolocation> element renders without requiring the user to press the button.

Examples

Basic usage

<geolocation></geolocation>
const geo = document.querySelector("geolocation");
console.log(geo.permissionStatus);
// "prompt" if this is the first time the user has accessed this page

Using permission status to inform the user

In our Embedded map example (source code), we add a HTMLGeolocationElement.promptaction_event event listener to the HTMLGeolocationElement object representing our <geolocation> element.

geo.addEventListener("promptaction", notifyUserGrantPermission);

In the referenced notifyUserGrantPermission() function, we use the permissionStatus property to check whether the permission status is denied or prompt and if so, we ask the user to press the button again and allow location. We don't need to ask this if they grant permission.

function notifyUserGrantPermission() {
  if (geo.permissionStatus === "denied" || geo.permissionStatus === "prompt") {
    statusElem.textContent =
      'Please press the "Use location" button again and allow location for this site.';
  }
}

See the main HTMLGeolocationElement page for a full walkthrough of this example.

Specifications

Browser compatibility

See also

  • geolocation element

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。