Thursday 19 October 2017

javascript - how to check whether localstorage is available?

Sometimes browsers block the access to the localStorage,
like when you are browsing in incognito mode or private mode and so on... So checking if
the storage exists is not the best idea, because sometimes it exists, but is not
accessible.


You can use this
function:


function storageON() {

try {
localStorage.setItem("__test", "data");
} catch (e)
{
return false;
}
return true;
}
if
(storageON()) { /* DO SOMETHING */ }

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...