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.
To get items from localStorage, use the getItem() method. getItem() allows you to access the data stored in the browser's localStorage object.
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.
That should work; the problem is that you have null in quotes.
Also, sessionStorage.length
will give you the number of values stored.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With