最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Javascript comparing 2 checks for localStorage - Stack Overflow

programmeradmin3浏览0评论

I saw this in the diveintohtml5 website. This is how they check to see if localstorage is supported on the browser.

return 'localStorage' in window && window['localStorage'] !== null;

Would this be the same as just doing?

return window.localStorage != undefined

I saw this in the diveintohtml5 website. This is how they check to see if localstorage is supported on the browser.

return 'localStorage' in window && window['localStorage'] !== null;

Would this be the same as just doing?

return window.localStorage != undefined

Share Improve this question asked Jan 16, 2011 at 15:54 AishwarAishwar 9,72411 gold badges62 silver badges84 bronze badges 3
  • personally I would do do return !!window.localStorage which would convert the values of 0,false,undefined,"",NaN to a false to do a better check – RobertPitt Commented Jan 16, 2011 at 16:18
  • 1 @Robert I think return typeof window.localStorage == 'object'; is the best method. If the localStorage property is not an object, then that means that it's not implemented in the browser – Šime Vidas Commented Jan 16, 2011 at 16:28
  • +1, this would be better as its more strict. – RobertPitt Commented Jan 16, 2011 at 16:30
Add a ment  | 

4 Answers 4

Reset to default 8

1

return 'localStorage' in window && window['localStorage'] !== null;

This returns true if the window object contains a property with the name localStorage and the value of that property is not null.


2

return window.localStorage != undefined

This returns true if the window object contains a propety with the name localStorage and the value of that property is not undefined or null (I am assuming that the global property undefined holds the value undefined)

Same result anyhow since if window.localStorage is undefined you will both get false. And if window.localStorage is null you will both get false because undefined == null.

However, I prefer using !! just because it's the fastest way to convert to a boolean and how useful is localStorage if it's false, null, undefined, '', NaN or 0?

return !!window.localStorage;

Edit One caveat, they are not exactly the same since if you set window.localStorage to undefined the first would report it as true

You can use Modernizr (1.1 or later) to detect support for HTML5 local storage.

if (Modernizr.localstorage) {
  // window.localStorage is available
} else {
  // no support for local storage
}

I wish you to remend the following function:

function getLocalStorage(){
   if (typeof localStorage == “object”){
      return localStorage;
   } else if (typeof globalStorage == “object”){
      return globalStorage[location.host];
   } else {
      throw new Error(“Local storage not available.”);
   }
}
  1. First check will be enough to make sure localStorage is availabe
  2. Some browsers still don't support local storage, but global storage. It has the same function set, but has some differences paring with localStorga
  3. If none of storages is supported, throw exception.

If you want to read about global storage, pare it with local storage, Look at "JavaScript for Web Developers", chapter 19. It describes client local storages, paring it with cookies.

发布评论

评论列表(0)

  1. 暂无评论