Adam Adam - 3 months ago 11
jQuery Question

jQuery: if condition is false, but still executed?

The following code

$(document).ready(function(){

sessionStorage.test = false;

alert( sessionStorage.test );

if ( sessionStorage.test ) {
alert("I dont care about your conditions!");
}

});


produces the following popups:


false

I dont care about your conditions!


even though
sessionStorage.test
is clearly set to false. Why is that the case? According to this answer http://stackoverflow.com/a/17986241/2311074, this should work. What am I doing wrong? I am using XAMPP - does it maybe have problems with sessionStorage? Here is the complete code of my test file:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script >
$(document).ready(function(){
sessionStorage.test = false;
if ( sessionStorage.test ) {
alert("I dont care about your conditions!");
}
});
</script>
</head>
<body>
<!-- page content -->
</body>
</html>

Answer

The default method for sessionsStorage is:

sessionStorage.setItem('keyNameee', "valueeee");    
sessionStorage.getItem('keyNameee')

however, note that it wont help even. sessionStorage returns value, not conditional boolean. It's same if you set:

sessionStorage.setItem('test',"false"); 

so, actual solution for you will be:

sessionStorage.setItem('test',false);    
if (sessionStorage.getItem('test') != "false") {
   ..........
}

Dublicate of Topics:
- sessionStorage setItem returns true or false
- sessionStorage isn't working as expected