permissions.query Gates Feature Access
Check navigator.permissions.query({ name }) before using a gated API, branch on granted/denied/prompt, and listen for runtime state changes.
Code
// permissions.query reports the current grant state without prompting
const status = await navigator.permissions.query({ name: 'geolocation' });
console.log('geolocation:', status.state);
// gate the feature path on the answer instead of calling and failing
if (status.state === 'granted') {
console.log('safe to watch position immediately');
} else if (status.state === 'denied') {
console.log('show settings help instead of a broken button');
}
// the state can change at runtime: 'change' fires on grant or revoke
status.addEventListener('change', () => {
console.log('geolocation changed:', status.state);
});What the snippet does
Every asynchronous step is awaited, so rejections surface where the call happens.
Top-level bindings status hold the demo state — nothing sits inside a function scope.
What to watch for
- The environment needs no preparation beyond having the interpreter or compiler.
- A browser is the runtime here:
navigatorare not available in Node.
Applying it
The run emits 4 labeled sections — geolocation:, safe to watch position immediately, show settings help instead of a broken button, geolocation changed: — one per step.
Statements that matter
status.addEventListener('change', () => {
console.log('geolocation changed:', status.state);
});
const status = await navigator.permissions.query({ name: 'geolocation' });
Interactive Tools for This Topic
Unix Permissions CalculatorConvert between octal and symbolic Unix file permissions and explain read/write/execute plus setuid, setgid, and sticky bits.Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.CORS Header Policy DebuggerSimulate browser CORS preflight and credential policies against server response headers to pinpoint blocked origins, methods, or headers and fix them.
FAQ
- Is the dependency story complicated?
- Not at all: zero dependencies, so there is nothing to version or audit. Its state lives in module-level bindings: `status`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.