The hasStorageAccess()
method of the Document
interface returns a Promise
that resolves with a boolean value indicating whether the document has access to unpartitioned cookies.
This method is part of the 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 unpartitioned cookies.
This method is part of the Storage Access API.
js
hasStorageAccess()
None.
A Promise
that resolves with a boolean value indicating whether the document has access to unpartitioned cookies — true
if it does, and false
if not.
The result returned by this method can be inaccurate in a couple of circumstances:
true
may be returned even though unpartitioned 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.false
may be returned even though unpartitioned 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.
InvalidStateError
DOMException
Thrown if the current Document
is not yet active.
js
document.hasStorageAccess().then((hasAccess) => { if (hasAccess) { // storage access has been granted already. } else { // storage access hasn't been granted already; // you may want to call requestStorageAccess(). } });
Note: See Using the Storage Access API for a more complete example.
Specification |
---|
The Storage Access API # dom-document-hasstorageaccess |
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
hasStorageAccess |
113Only available to Google Chrome's first-party sets. |
85 | 65 | No |
99Only available to Opera's first-party sets. |
11.1 | 113Only available to Google Chrome's first-party sets. |
113Only available to Google Chrome's first-party sets. |
65 | No | 11.3 | No |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Document/hasStorageAccess