Files
30-seconds-of-code/snippets/is-session-storage-enabled.md
Angelos Chalaris 61200d90c4 Kebab file names
2023-04-27 21:58:35 +03:00

766 B

title, tags, author, cover, firstSeen, lastUpdated
title tags author cover firstSeen lastUpdated
Check if sessionStorage is enabled browser chalarangelo flower-camera 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