Tiny Frontend Logo
Posts 0124

On This Page

2. Methods
5. Usage

Permissions on a Webpage

Your website cannot readily use some services like camera, microphone, location, etc. on the user device. For security reasons, the websites need to specifically request for permissions from the user to use these services. In this post, let's learn how to handle these permissions on a webpage.

Permissions API

Using the Permissions API, the webpage can check whether it can access a particular service. It is a centralised and consistent way to check for the permissions.

The permissions object can be accessed using the permissions property in the navigator object. This should be present in most browsers but if it's not, you'll need to use the methods provided by the specific WebAPI, for example, the Camera API, to check and request for permissions.

if(navigator.permissions){
    // ... query for the permission
} else{
    // permissions object not available!
    // Use the specific WebAPI to check
    // and request for the permissions.
}

Methods

query(permissionDescriptor)

The query method accepts a permissionDescriptor object which contains the following properties:

  • name: string: The name of the WebAPI whose permission you want to check.

The method returns a Promise that resolves with a PermissionStatus object.

If the permission name passed to the query method doesn't exist, it throws a TypeError so make sure to handle errors either using a try-catch block or .catch clause.

navigator.permissions.query({ name: "camera" })
    .catch((e) => {
        // handle errors
    });
Subscribe to read the full content

* Free subscribe to enjoy full content access.
* You can cancel anytime with 1-click.

Read Next

0123

Network Requests in JavaScript

One of the crucial and fundamental component of a website is to make Network Requests to fetch some data. There are a lot of libraries available now, like axios, that make this task very easy to implement for the developers. But, if you've ever wondered what goes on underneath those libraries or if you want to achieve this using pure JavaScript, keep reading.

Network Requests in JavaScript
0122

Iterators and Generators in JavaScript

Let's discover some hidden gems and not so commonly used concepts in JavaScript. Iterators and Generators bring the concept of iteration directly into the core language and provide a mechanism for customizing the behavior of for...of loops.

0121

Lazy Load Offscreen Iframes

As more websites embed YouTube videos into their pages to provide richer content, and it indeed helps users access information more effectively.

Lazy Load Offscreen Iframes
0120

Resumable File Upload

The cloud storage has become an indispensable file backup and sharing solution in our daily work. But have you ever considered:

Resumable File Upload
0119

Sending Notifications

The Notification API allows web developers to create and display desktop notifications to users.

Sending Notifications
0118

Higher-Order Functions (HOF) in JavaScript

In JavaScript, functions are treated as first-class citizens. We can use functions as values, assign them to variables, pass them as arguments to other functions, or even return them from other functions.

Higher-Order Functions (HOF) in JavaScript