HTML5 特性检测:本地存储(Local Storage ,sessionStorage)
如果你的浏览器支持该特性的话,那么全局对象:window上会有一个localStorage的属性,反之,你的浏览器不支持的话,那么该属性值为undefined
function supports_local_storage(){ return !!window.localStorage; }
<input type="button" onclick="Supports_localStorage()" value="Supports_localStorage"/><script type="text/javascript"> function Supports_localStorage() { alert(window.localStorage); }</script>
<input type="button" onclick="Supports_sessionStorage()" value="Supports_sessionStorage"/><script type="text/javascript"> function Supports_sessionStorage() { alert(window.sessionStorage); }</script>
同样的,如果你不想自己亲手去写这个检测方法的话,你可以使用Modernizr来检测你的浏览器是否支持本地存储。
if(Modernizr.localstorage){ //window.localStorage is available! }else{ //no native support for local storage //maybe try Gears or another third-party solution }