Friday, May 10, 2024
192
rated 0 times [  193] [ 1]  / answers: 1 / hits: 28972  / 9 Years ago, thu, june 4, 2015, 12:00:00

I am using this piece of code inside a javascript function.
But when I run the application it gives a reference error that local Storage is not defined.



localStorage.setItem(result.key+authAppParams.application , result.key);


Why this occurs?
Why can't I use this inside a function?
Since it is a setItem and all the releated values are inside the method I cannot define the local Storage globally.



How can I avoid this issue and make this work?


More From » local-storage

 Answers
1

When using localStorage you should always check for it first to make sure the browser has it implemented.


Something like this


if(window.localStorage) {
// localStorage can be used
} else {
// can't be used
}

Or, if this errors in your browser (which it may not), you can check with


if('localStorage' in window) {
// localStorage can be used
} else {
// can't be used
}

Should probably check for it early on in your code and only use it if it's available.


JS will always look on the global for a variable that it cannot find in the current context, so it looks as if it comes down to the browser not having it.


Edit Like everyone is commenting, localStorage and jQuery are completely separate


[#66334] Tuesday, June 2, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
breonnamayah

Total Points: 574
Total Questions: 115
Total Answers: 96

Location: England
Member since Sun, May 21, 2023
1 Year ago
;