docs.rodeo

MDN Web Docs mirror

Permissions: query() method

{{APIRef("Permissions API")}} {{AvailableInWorkers}} 

The query() method of the {{domxref("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

query(permissionDescriptor)

Parameters

Return value

A {{jsxref("Promise")}}  that resolves to a {{domxref("PermissionStatus")}}  object.

Exceptions

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.

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.

<pre id="log"></pre>
const logElement = document.querySelector("#log");
function log(text) {
  logElement.innerText = `${logElement.innerText}${text}\n`;
  logElement.scrollTop = logElement.scrollHeight;
}
#log {
  height: 320px;
  overflow: scroll;
  padding: 0.5rem;
  border: 1px solid black;
}
// 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:

{{EmbedLiveSample('Test support for various permissions',"100%", "370px")}} 

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

In this article

View on MDN