local Storage is not defined : JQuery [closed]

2019-09-11 05:39发布

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?

1条回答
Anthone
2楼-- · 2019-09-11 06:27

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
}

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

查看更多
登录 后发表回答