Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: if condition is false, but still executed? [duplicate]

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 https://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>
like image 948
Adam Avatar asked Mar 12 '23 08:03

Adam


1 Answers

The default getter/setter for sessionsStorage are:

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

So, for example:

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

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

like image 133
T.Todua Avatar answered Apr 27 '23 00:04

T.Todua