测试代码参考: 复制代码代码如下: !DOCTYPE html html head script function log( msg ) { console.log(msg); alert(msg); } /p p var limit; var half = '1'; //这里会换成中文再跑一遍 var str = half; var sstr; while ( 1 ) { try { localStorage.clear(); str += half; localStorage.setItem( 'cache', str ); half = str; } catch ( ex ) { break; } } var base = str.length; var off = base / 2; var isLeft = 1; while ( off ) { if ( isLeft ) { end = base - (off / 2); } else { end = base + (off / 2); } /p p sstr = str.slice( 0, end ); localStorage.clear(); try { localStorage.setItem( 'cache', sstr ); limit = sstr.length; isLeft = 0; } catch ( e ) { isLeft = 1; } /p p base = end; off = Math.floor( off / 2 ); } /p p log( 'limit: ' + limit ); /script /html