2011/9/22 Anders Jönsson <[email protected]>: > Hi > > On "http://diveintohtml5.org/storage.html" they use the following code to > check for local storage support: > > function supports_html5_storage() { > try { > return 'localStorage' in window && window['localStorage'] !== null; > } catch (e) { > return false; > } > } > > What's the difference between that way and this way?:
They are explicitly checking for null. The typeof operator currently returns Object for null types. So this is the only way to safely explicitly check for a null value. > And can we also remove the try...catch?: I'm guessing that reading localStorage in some engines is or was "poisoned", throwing an error when read. But I don't have any details on that. That's the only thing I can think of anyways, aside from `window` not being what we expect for some reason. > Is it more safe to use "'localStorage' in window && window['localStorage'] > !== null; " over "typeof"? If you are checking for null, yes. Also, note that window['localStorage'] is EXACTLY the same as window.localStorage (for cases where the string would otherwise be a valid identifier). There is no engine I know of that does this wrong. So unless you want to use something exotic as property name, try to use the dot instead :) - peter -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
