Skip to content
the poor coder

check if browser supports Local Storage

Poor Man
Published
Reading time
2 min read
On this page

How to Check if Browser Supports Local Storage in HTML#

If you are writing web applications that support offline data storage, then you will need to know if the browser supports local storage or not. Here's how you can check if the browser supports local storage:

Method 1: Using JavaScript#

Text

if (typeof(Storage) !== "undefined") {
  // Local storage is supported
  // Your code here
} else {
  // Local storage is not supported
  // Your code here
}

The above code checks if the Storage object is defined or not. If it is not undefined, it means that local storage is supported by the browser. You can then write your code for storing and retrieving data using local storage.

Method 2: Using Modernizr#

Modernizr is a JavaScript library that detects HTML5 and CSS3 features in the user's browser. You can use Modernizr to detect if local storage is supported or not:

Text

if (Modernizr.localstorage) {
  // Local storage is supported
  // Your code here
} else {
  // Local storage is not supported
  // Your code here
}

The above code checks if the localstorage property of the Modernizr object is true or false. If it is true, it means that local storage is supported by the browser.

Method 3: Using try-catch block#

You can also check if local storage is supported by wrapping your code in a try-catch block:

Text

try {
  localStorage.setItem('test', 'test');
  localStorage.removeItem('test');
  // Local storage is supported
  // Your code here
} catch (e) {
  // Local storage is not supported
  // Your code here
}

The above code tries to set and then remove an item from local storage. If there are no errors, it means that local storage is supported by the browser.

These are the three methods that you can use to check if the browser supports local storage. Choose the one that suits your needs and implement it in your code.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC