PermissionStatus:state 属性

Baseline 2022
Newly available

Since September 2022, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

备注: 此特性在 Web Worker 中可用。

PermissionStatus 接口的 state 只读属性返回所请求权限的状态。此属性返回 'granted''denied''prompt' 之一。

以下值之一:

'granted'

用户或代表用户的用户代理已明确许可使用此技术特性。调用者可以使用该特性,而无需用户代理请求用户的许可。

'denied'

用户或代表用户的用户代理已拒绝访问此技术特性。调用者无法使用该特性。

'prompt'

用户未明确授予使用该特性的权限(即与拒绝相同)。这还意味着,如果调用者尝试使用该特性,用户代理将提示用户授予许可,否则对该特性的访问将被拒绝。

示例

js
navigator.permissions
  .query({ name: "geolocation" })
  .then((permissionStatus) => {
    console.log(`geolocation 权限状态为 ${permissionStatus.state}`);
    permissionStatus.onchange = () => {
      console.log(`geolocation 权限状态已更改为 ${permissionStatus.state}`);
    };
  });

规范

Specification
Permissions
# dom-permissionstatus-state

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
state

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Uses a non-standard name.
Has more compatibility info.