H5中的localstorage缓存对象

以前可以用cookie来缓存浏览器内容的,html5提供了一种更简单的缓存方式localstorage(无限期的缓存).

localstorage是浏览器内置对象,使用很简单,比如你要存一个{'city': 'BeiJing'}就直接用localstorage.city = ‘BeiJing’,

使用的时候localstorage.city就可以获取到'BeiJing'。

值得注意的是,有些用户可能关闭了浏览器的localstorage功能,这种情况下再使用就会报错,所以最好使用try-catch语句包裹。

例如



猜你喜欢

转载自blog.csdn.net/lmhlmh_/article/details/80990151