3

If I use this:

<script>
    $(document).ready(function() {
        if (window.localStorage.getItem('createTicket')) {
            var createTicketInfo = JSON.parse(window.localStorage.getItem('createTicket'));
        }
    });
</script

I see an error in the console:

Uncaught ReferenceError: createTicketInfo is not defined

enter image description here

But if I remove document.ready:

<script>    
    if (window.localStorage.getItem('createTicket')) {
        var createTicketInfo = JSON.parse(window.localStorage.getItem('createTicket'));
    }   
</script>

Everything is ok:
enter image description here

Why?

Pang
  • 9,564
  • 146
  • 81
  • 122
Evgeniy
  • 31
  • 2
  • Related problem dealing with variable scopes: https://stackoverflow.com/questions/11152977/global-javascript-variable-inside-document-ready – Jedi Jun 23 '17 at 02:46

2 Answers2

4

It's because, in the first example, createTicketInfo is only visible within the callback (function) of ready.

function test() {
  var boo = "Hi! I'm boo.";
  // ...
  console.log(boo); // visible here
}
test();

console.log(boo); // not visible here (even if we call test first)

In the second example, createTicketInfo is not wrapped in a function, thus it is global (visible everywhere).

if(true) {
  if(true) {
    if(true) {
      var foo = "Hi! I'm foo.";
      // ...
      console.log(foo); // visible here
    }
  }
}

console.log(foo); // visible here too (because javascript doesn't have block scopes)

Always put in mind that javascript has function scopes, but not block scopes.

Note: As mentioned by @nnnnnn in the comment bellow, ECMAScript 6 introduced a new way of declaring variables using let or const that respect block scopes. So:

if(true) {
  let zoo = "Hi! I'm zoo.";
  // ...
  console.log(zoo); // only visible inside this block
}

console.log(zoo); // not visible here
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
0

Something else isn't right in another section of your code.

Look at this fiddle I created.

It accurately reads and writes the value. Exactly the same code:

https://jsfiddle.net/nxka5h7y/2/

<head>
<script src = "https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
    window.localStorage.setItem('createTicket', '{"a":"1"}');
    $(document).ready(function() {
        if (window.localStorage.getItem('createTicket')) {
            var createTicketInfo =     JSON.parse(window.localStorage.getItem('createTicket'));
            console.log(createTicketInfo);
        } else { console.log("none"); }
    });
</script>
</head>
<body>
<script>
</body>

The only thing I added, was actually setting the createTicket item in localStorage, just to ensure the "if" statement inside document/ready will pass.

My semi-logical guess is that JSON stored in createTicket is malformed, based on how it was set (which is not shown in your example.) I don't really think that's the issue. But, the fact is your example produces accurate results in jFiddle. Look elsewhere in your code. Like, the part where you actually set the createTicket. Or that your jQuery is actually included, or something silly like that.

InfiniteStack
  • 430
  • 2
  • 9