Permissions: query() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2022.

Note: This feature is available in Web Workers.

The query() method of the Permissions interface returns the state of a user permission on the global scope.

The user permission names are defined in the respective specifications for each feature. The permissions supported by different browser versions are listed in the compatibility data of the Permissions interface (see also the relevant source code for Firefox values, Chromium values, and WebKit values).

The APIs that are gated by each permission are listed in Permission-aware APIs in the Permissions API overview topic.

Syntax

js
query(permissionDescriptor)

Parameters

permissionDescriptor

An object that sets options for the query operation. The available options for this descriptor depend on the permission type.

All permissions have a name:

name

A string containing the name of the API whose permissions you want to query, such as camera, bluetooth, microphone, geolocation (see Permissions for a more complete list). The returned Promise will reject with a TypeError if the permission name is not supported by the browser.

For the push permissions you can also specify:

userVisibleOnly Optional

(Push only, not supported in Firefox — see the Browser Support section below) Indicates whether you want to show a notification for every message or be able to send silent push notifications. The default is false.

For the midi permission you can also specify:

sysex Optional

Indicates whether you need and/or receive system exclusive messages. The default is false.

Return value

A Promise that resolves to a PermissionStatus object.

Exceptions

InvalidStateError DOMException

Thrown if query() method is invoked in the browsing context and its associated document is not fully active.

TypeError

Thrown if retrieving the PermissionDescriptor information failed in some way, or the permission doesn't exist or is unsupported by the user agent.

Examples

Display news based on geolocation permission

This example shows how you might display news related to the current location if the geolocation permission is granted, and otherwise prompt the user to enable granting access to the location.

js
navigator.permissions.query({ name: "geolocation" }).then((result) => {
  if (result.state === "granted") {
    showLocalNewsWithGeolocation();
  } else if (result.state === "prompt") {
    showButtonToEnableLocalNews();
  }
  // Don't do anything if the permission was denied.
});

Test support for various permissions

This example shows the result of querying each of the permissions.

The code uses navigator.permissions.query() to query each permission, logging either the permission status or the fact that the permission is not supported on the browser. Note that the query() is called inside a try...catch block because the associated Promise will reject if the permission is not supported.

js
// Array of permissions
const permissions = [
  "accelerometer",
  "accessibility-events",
  "ambient-light-sensor",
  "background-sync",
  "camera",
  "clipboard-read",
  "clipboard-write",
  "geolocation",
  "gyroscope",
  "local-fonts",
  "magnetometer",
  "microphone",
  "midi",
  "notifications",
  "payment-handler",
  "persistent-storage",
  "push",
  "screen-wake-lock",
  "storage-access",
  "top-level-storage-access",
  "window-management",
];

processPermissions();

// Iterate through the permissions and log the result
async function processPermissions() {
  for (const permission of permissions) {
    const result = await getPermission(permission);
    log(result);
  }
}

// Query a single permission in a try...catch block and return result
async function getPermission(permission) {
  try {
    let result;
    if (permission === "top-level-storage-access") {
      result = await navigator.permissions.query({
        name: permission,
        requestedOrigin: window.location.origin,
      });
    } else {
      result = await navigator.permissions.query({ name: permission });
    }
    return `${permission}: ${result.state}`;
  } catch (error) {
    return `${permission} (not supported)`;
  }
}

The log from running the code is shown below:

Specifications

Specification
Permissions
# dom-permissions-query

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
query
Chrome – Full support
Chrome 43 (Release date: 2015-05-19)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 46 (Release date: 2016-04-26)
footnote Full support
Opera – Full support
Opera 30 (Release date: 2015-06-09)
footnote Full support
Safari – Full support
Safari 16 (Release date: 2022-09-12)
footnote Full support
Chrome Android – Full support
Chrome Android 43 (Release date: 2015-05-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 46 (Release date: 2016-04-26)
footnote Full support
Opera Android – Full support
Opera Android 30 (Release date: 2015-06-10)
footnote Full support
Safari on iOS – Full support
Safari on iOS 16 (Release date: 2022-09-12)
footnote Full support
Samsung Internet – Full support
Samsung Internet 4 (Release date: 2016-03-11)
footnote Full support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – Full support
WebView on iOS 16 (Release date: 2022-09-12)
footnote Full support

Legend

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

Full support
Full support
No support
No support