Local Storage Not Working


#1

I am trying to create a global variable that can be accessed throughout the entire document. From what I could find on the forum it looked like the way to do this was with local storage. So I tried to write this code:

localStorage.setItem(‘prev’, hypeDocument.currentSceneName());
hypeDocument.showSceneNamed(“List”);

I want it to create a global variable called “prev” and set it to the name of the current scene. Then I want to display another scene called “List”. But when I try to run the program, it doesn’t seem to be working (as it never displays the new scene). I’m pretty sure the problem is in the local storage command, because the code works if I take that out. But I’m not sure what’s wrong with it / how to fix it?

On a semi-related note, is there a debugger for javascript code in tumult hype?


#2

In the code you shared, it looks like you have smart quotes around ‘prev’ – can you make sure you type them with the keyboard instead of copying from elsewhere?

localStorage.setItem('prev',  hypeDocument.currentSceneName());
hypeDocument.showSceneNamed("List");

There isn’t, but you can use console.log or alert() to inspect your code as you go.


#3

Thank you!


(Mark Hunte) #4

Just wondering if you really need localStorage.

We normally assign a global var by either initiating it without the var tag or attaching it to the window scope directly.
i.e

var foo = "sometext"

This is local to the scope of the function/block it is in and not global

foo = "sometext"
and
window.foo = "sometext"

are both the same and global to the scope of the browser’s window

( afaik the common reason to directly attach to the window using the dot syntax is to show an intentional global rather than one where we have forgotton the var tag. )

One thing to remember is that the global var needs to be defined /initiated before you try and access it or you will get an error.

I normally guard againest this by checking if it defined and if not define it once and in the function that I am going to access it in.

i.e

if (typeof window.foo == "undefined") { window.foo = "" }