Categories
JavaScript Answers

How to check if geolocation has been declined with JavaScript?

Spread the love

Sometimes, we want to check if geolocation has been declined with JavaScript.

In this article, we’ll look at how to check if geolocation has been declined with JavaScript.

How to check if geolocation has been declined with JavaScript?

To check if geolocation has been declined with JavaScript, we can use the navigator.permissions.query method.

For instance, we write

const result = await navigator.permissions.query({ name: "geolocation" });
console.log(result.state);

to call navigator.permissions.query with an object with the name property set to 'geolocation' to check if the app has permission to use geolocation.

It returns a promise that has whether permission is granted or not.

If result.state is 'granted', then permission is granted.

Conclusion

To check if geolocation has been declined with JavaScript, we can use the navigator.permissions.query method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *