diveintomark / diveintohtml5

Dive Into HTML5 online book
https://diveintohtml5.info/
Other
777 stars 187 forks source link

Comparing with null object #86

Closed pablovezgz closed 7 years ago

pablovezgz commented 7 years ago

In this section: http://diveintohtml5.info/storage.html#localstorage

function supports_html5_storage() {
  try {
    return 'localStorage' in window && window['localStorage'] !== null;
  } catch (e) {
    return false;
  }
}

The second operator at the first return statement always returns true. window['localStorage'] !== null; should be compared to undefined

schlimmchen commented 7 years ago

Internet Explorer, when viewing local files using a file:// URI, will have window['localStorage'] == undefined (due to policy?!).

So I affirm that the comparison against undefined must be added. However, removing the comparison against null triggers my paranoia. Why should we replace the current comparison? My guess is the comparison against undefined should be added.

paulirish commented 7 years ago

fixed in https://github.com/diveintomark/diveintohtml5/commit/0bbd404c088743963fd3e7f8f74923d1d09c0cf6

i just used the detect from modernizr for all the reasons. https://github.com/Modernizr/Modernizr/blob/d5f881a4de0d5fc1af85921ce9c7dc3919c6d335/feature-detects/storage/localstorage.js#L20-L49