Browser Bugs and Inconsistencies in localStorage

Even though localStorage has been around for several years, there are still inconsistencies across browsers when it comes to both removing items and reporting which items exist as members. IE8, Firefox Desktop <= v13, Firefox Mobile, Opera and Opera Mobile all suffer from problems.

Non-Existent Item Values

In all tested browsers that support localStorage, a call to localStorage.getItem for an item that does not exist returns null. In Firefox (versions <= 13), a call to localStorage.<non_existent_item_name> will return null as well.

Firefox's discrepency has been fixed in Firefox 14, which is in the Aurora Channel at the time of writing this article.

Using the in Operator on Non-Existent Items

Both Firefox (versions <= 13) and Opera fail using the in operator. Opera appears to always misreport an item as being in localStorage. Firefox will report an item as being in localStorage after the item is removed using removeItem. Firefox will correctly update its state when the page is reloaded.

localStorage.hasOwnProperty

Since in appears to be a no-go for a subset of browsers, perhaps localStorage.hasOwnProperty could help out. It turns out that IE8 does not support hasOwnProperty on localStorage. Opera returns true for every property that is queried.

Removing Items - delete vs localStorage.removeItem

The entire impetus for this post started in a code review where I saw an author do a mistake that I have been bitten by in the past - removing items from localStorage using the delete operator. Unfortunately, IE8 will throw an exception if the item being deleted is not already in localStorage.

This is a problem, because it is not possible to reliably query whether an item is in localStorage in all browsers. As noted above, both Firefox and Opera can return true using the in operator and IE8 and Opera both fail using localStorage.hasOwnProperty.

What About Mobile?

Mobile Safari on iOS 5.1 and the Default Android browser on Honeycomb have passed all the tests. The current Mobile Firefox (Fennec) reports the same failures as Firefox 12/13.

Where Are The Tests?

You can see the tests at JSFiddle.

Leave a Comment

Post comment

What is Persona?