I just followed this example https://developer.mozilla.org/en-US/docs/Web/API/Document/requestStorageAccess#examples to request localStorage access for a cross domain iframe. This works in Chrome. but in Firefox the handle
is undefined. I can confirm that the localStorage access has been granted. Any idea why the handle
is undefined in Firefox
document.requestStorageAccess({ localStorage: true }).then(
(handle) => {
console.log("localStorage access granted");
handle.localStorage.setItem("foo", "bar");
},
() => {
console.log("localStorage access denied");
},
);
2
Answers
The types parameter (in your case
{localStorage: true}
) is not yet(?) supported in Firefox. See the Browser Compatibility.Your
handle
variable becomes undefined because the access to third-party cookies was granted and nottypes
parameter was provided. Not because you didn’t specify it, just because Firefox doesn’t support it. (See here)I think maybe your problem lies with how you’re trying to call
requestStorageAccess()
…From https://developer.mozilla.org/en-US/docs/Web/API/Document/requestStorageAccess#return_value
Make sure the function is called inside an event listener like an
onclick
function.By the way, as it was an example from
MDN
(which is the developper of FireFox), it shouldn’t fail on FireFox… Unless you’re not doing it right…