local Storage is not defined : JQuery [closed]

北城余情 提交于 2020-01-15 15:29:32

问题


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:


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



来源:https://stackoverflow.com/questions/30641571/local-storage-is-not-defined-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!