Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if HTML sessionStorage is not empty?

G'day guys,

I'm trying to figure out the best way to check for empty storageSession in HTML5.

I noticed that some browser returns null and some return just empty space if you clear or did not set any values to the sessionStorage.

I've tried the following

if (sessionStorage.getItem('value1') == "null" && sessionStorage.getItem('value2') == "null") {

But it doesn't seem to work.

Anyone got a better way to check if a sessionStorage is empty?

Appreciate your help on this and thank you in advance.

like image 998
John Citizen Avatar asked Jan 16 '11 09:01

John Citizen


People also ask

How do I check local storage in HTML?

To get items from localStorage, use the getItem() method. getItem() allows you to access the data stored in the browser's localStorage object.

When session storage is cleared?

sessionStorage is similar to localStorage ; the difference is that while data in localStorage doesn't expire, data in sessionStorage is cleared when the page session ends. Whenever a document is loaded in a particular tab in the browser, a unique page session gets created and assigned to that particular tab.


2 Answers

That should work; the problem is that you have null in quotes.

Also, sessionStorage.length will give you the number of values stored.

like image 57
Herohtar Avatar answered Oct 05 '22 23:10

Herohtar


i checked this way

   $(document).ready(function () {
       if (window.sessionStorage) {
           if (sessionStorage.getItem("x-mas") === null) {
               if (getCountryCode() == 'de') {
                   $('#xmasModal').modal();
                   sessionStorage.setItem("x-mas", "1")
               }
           }
       }
   });

   function getCountryCode() {
       var url = window.location.pathname;
       var segments = url.split('/');
       var countrycode = segments[1];
       return countrycode;
   }
like image 21
Thomas Avatar answered Oct 06 '22 00:10

Thomas