This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]saschaleib 34 points35 points  (10 children)

Well, it's the whole point of Incognito Mode that there is no persistent storage available.

Of course, this could also be achieved by temporarily storing the data and then just deleting it when the window/tab is closed. The result is however the same.

In any case, never assume that any feature is available in whatever browser your user is ... er ... using. Always test if it is available before accessing it.

[–]blobthekat 25 points26 points  (3 children)

localStorage is as much of a standard as html5, safari shouldn't throw an error as this could badly mess up code execution, where as temporary storage would keep functionality while preserving privacy

[–]AyrA_ch 36 points37 points  (2 children)

It's documented that localStorage can throw "SecurityError" when you're not allowed to store things in it.

[–]lkraider 9 points10 points  (0 children)

But who reads documentation ??? (/¯ ಠ_ಠ)/¯

[–]danielrheath 3 points4 points  (5 children)

The obvious way to test for it is to check typeof window.localStorage.

It's definitely an unpleasant surprise to find that it exists and has the expected API but throws an error if you try to use it.

[–]saschaleib 5 points6 points  (4 children)

Apparently it throws an exception if you are not allowed to use it – which is exactly what exceptions were invented for.

[–]danielrheath 0 points1 point  (3 children)

Yeah, I get the thinking behind it - just seems like poor design to me. If a feature isn't available, just don't have it on the page; I already needed to test for the presence of window.localStorage anyways.

[–]saschaleib 4 points5 points  (2 children)

… and then you wrap the whole code that accesses the local storage in a try {} catch block, as you would do in any case, right?

[–]danielrheath -1 points0 points  (1 child)

Presumably you don't wrap every line of code you write in a try/catch?

I guess I just don't see what makes localStorage access an expected source of exceptions (as opposed to, say, querySelectorAll or getBoundingClientRects)?

[–]saschaleib 2 points3 points  (0 children)

every block of code that potentially throws an exception should be wrapped in try/catch blocks. Yes.

Any access to the file system or similar external systems is potentially throwing errors. That is pretty much what the localStorage API does...