e01.org

Document: hasStorageAccess() method

Storage Access API

Storage Access API

The **`hasStorageAccess()`** method of the Document interface returns a Promise that resolves with a boolean value indicating whether the document has access to [third-party](/docs/Web/Privacy/Guides/Third-party_cookies), [unpartitioned](/docs/Web/API/Storage_Access_API#unpartitioned_versus_partitioned_cookies) cookies.

This method is part of the [Storage Access API](/docs/Web/API/Storage_Access_API).

> [!NOTE]
> This method is another name for Document.hasUnpartitionedCookieAccess(). There are no current plans to remove this method in favor of Document.hasUnpartitionedCookieAccess().

## Syntax

```js-nolint
hasStorageAccess()
```

### Parameters

None.

### Return value

A Promise that resolves with a boolean value indicating whether the document has access to third-party cookies — `true` if it does, and `false` if not.

The result returned by this method can be inaccurate in a couple of circumstances:

1. The user may have active browser settings that block third-party cookies; in this case, `true` may be returned even though third-party cookies are still inaccessible. To handle such a situation, you should gracefully handle any errors resulting in cookie values being unretrievable; for example, inform the user that access to their personalized settings is blocked and invite them to sign in again to use them.
2. The browser might not block third-party cookie access by default; in this case, `false` may be returned even though third-party cookies are accessible, and storage access wouldn't need to be requested (i.e., via Document.requestStorageAccess()). To get around this issue, you could query Document.cookie to find out whether your cookies are accessible, and call Document.requestStorageAccess() if they are not.

> [!NOTE]
> If the promise gets resolved and a user gesture event was being processed when the function was originally called, the resolve handler will run as if a user gesture was being processed, so it will be able to call APIs that require user activation.

### Exceptions

- `InvalidStateError` DOMException
- : Thrown if the current Document is not yet active.

## Examples

```js
document.hasStorageAccess().then((hasAccess) => {
if (hasAccess) {
// storage access has been granted already.
console.log("cookie access granted");
} else {
// storage access hasn't been granted already;
// you may want to call requestStorageAccess().
console.log("cookie access denied");
}
});
```

> [!NOTE]
> See [Using the Storage Access API](/docs/Web/API/Storage_Access_API/Using) for a more complete example.

## Specifications

## Browser compatibility

## See also

- Document.hasUnpartitionedCookieAccess(), Document.requestStorageAccess(), Document.requestStorageAccessFor()
- [Using the Storage Access API](/docs/Web/API/Storage_Access_API/Using)
- [Introducing Storage Access API](https://webkit.org/blog/8124/introducing-storage-access-api/) (WebKit blog)

source: MDN Web Docs mirror · page

Cite this

"Document: hasStorageAccess() method". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/API/Document/hasStorageAccess

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·