JQuery: if the condition is false but still satisfied?

Following code

$(document).ready(function(){ sessionStorage.test = false; alert( sessionStorage.test ); if ( sessionStorage.test ) { alert("I dont care about your conditions!"); } }); 

Creates the following pop-ups:

falsely

I don't care about your conditions!

although sessionStorage.test explicitly set to false. Why is this so? In accordance with this answer, https://stackoverflow.com/a/3606186/2126, this should work. What am I doing wrong? I am using XAMPP - maybe you have problems with sessionStorage? Here is the complete code for 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> 
+1
jquery session-storage
source share
1 answer

Default sessionSorage method:

 sessionStorage.setItem('keyName', "value"); sessionStorage.getItem('keyName') 

However, note that this will not help. sessionStorage returns the value, not the conditional value boolean. It acts as if you set:

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

So, the actual solution for you will be:

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

Recurring topics:
- sessionStorage setItem returns true or false
- sessionStorage is not working properly

+2
source share

All Articles