Files
30-seconds-of-code/snippets/isSessionStorageEnabled.md
Isabelle Viktoria Maciohsek 27c168ce55 Bake date into snippets
2021-06-13 13:55:00 +03:00

726 B

title, tags, firstSeen, lastUpdated
title tags firstSeen lastUpdated
isSessionStorageEnabled browser,intermediate 2020-12-31T13:13:47+02:00 2020-12-31T13:13:47+02:00

Checks if sessionStorage is enabled.

  • Use a try...catch block to return true if all operations complete successfully, false otherwise.
  • Use Storage.setItem() and Storage.removeItem() to test storing and deleting a value in window.sessionStorage.
const isSessionStorageEnabled = () => {
  try {
    const key = `__storage__test`;
    window.sessionStorage.setItem(key, null);
    window.sessionStorage.removeItem(key);
    return true;
  } catch (e) {
    return false;
  }
};
isSessionStorageEnabled(); // true, if sessionStorage is accessible