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开发者_JS百科
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.
That should work; the problem is that you have null in quotes.
Also, sessionStorage.length
will give you the number of values stored.
But this is how you check for "empty" session storage
if (sessionStorage.length == 0) {
Use this to check if there is item is session storage called "name"
if (sessionStorage['name']) {
console.log("There is 'name' in session storage ")
}
You can also try
if($window.sessionStorage.value === undefined) {}
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;
}
In case like this, I use:
if (sessionStorage.getItem('value1').length == 0) {}
精彩评论