Thursday, December 20, 2018

javascript - Check for HTML 5 localStorage



How to check for HTML 5 localStorage compability of the users browser?



I have been using two checks:



    if (window.localStorage) {

// localStorage works!
}


and



    if (typeof(localStorage) != 'undefined') {
// localStorage works!
}



Also, I have seen a function online which uses an try/catch block to check whether localStorage can add a key/data pair, or throws an error.



Which of those three methods would you recommend, and are there any differences between the first and the second?


Answer



@MathiasBynens has written a nice article about this, comparing different approaches:



http://mathiasbynens.be/notes/localstorage-pattern


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...