Home > Article > Web Front-end > Xiaoqiang’s HTML5 mobile development road (19) - HTML5 Local Storage (local storage)
1. The development history of browser storage
There are many local storage solutions, such as Flash SharedObject, Google Gears, Cookie, DOM Storage, User Data, window.name, Silverlight, Open Database, etc.
Let’s borrow a picture from the Internet to take a look at the current mainstream local storage solutions:
Cookie: is widely used in the web, but has very limited limitations. Obviously, the capacity is too small. Some sites will disable cookies for security reasons. Cookies are not as safe as imagined. The content of the cookie will be sent to the server along with the page request.
Flash SharedObject: Kissy’s store module is used to call Flash SharedObject. The advantage of Flash SharedObject is that it has moderate capacity and basically no compatibility issues. The disadvantage is that it requires the introduction of specific swf and js files into the page, which adds extra burden and is cumbersome to process; some machines do not have a flash operating environment.
Google Gears: Google’s offline solution has stopped updating. The official recommendation is to use the HTML5 localStorage solution.
User Data: It is a storage space specially opened by Microsoft in the system for IE, so it only supports the combination of Windows+IE. The actual test was in 2000 (IE5.5), XP (IE6, IE7). It can be used normally under Vista (IE7). Under XP, it is usually located in C:\Documents and Settings\username\UserData, sometimes it is in C:\Documents and Settings\username\Application Data\Microsoft\Internet Explorer\UserData. Under Vista, it is located in C:\Users\Username\AppData\Roaming\Microsoft\Internet Explorer\UserData; the size limit of a single file is 128KB, and a total of 1024KB files can be saved under one domain name. There should be no limit on the number of files. In restricted sites, these two values are 64KB and 640KB respectively, so if various circumstances are taken into account, it is best to control a single file to be less than 64KB.
localStorage: Compared with the above local storage solutions, localStorage has its own advantages: large capacity, easy to use, powerful, native support; the disadvantage is poor compatibility (chrome, safari, firefox, IE 9, IE8 all support localStorage, mainly not supported by versions below IE8) and less secure (so please do not use localStorage to save sensitive information).
The browser compatibility of localStorage in Html5 is as follows:
##localStorage.t1;
localStorage["t2"];
localStorage.getItem("t3");
<!DOCTYPE HTML> <html> <head> <meta charset="urf-8"/> </head> <body> <script type="text/javascript"> //判断浏览器是否支持本地存储 if(window.localStorage){ localStorage.t1="大碗干拌"; document.write(localStorage.t1); localStorage['t2']="<br/>hello word" document.write(localStorage.t2); localStorage.setItem("t3", "<br/>http://blog.csdn.net/dawanganban"); document.write(localStorage.t3); }else{ alert("你的浏览器不支持"); } </script> </body> </html>
Comment out the above three lines of assignment code, you will find that the data can still be displayed on the browser.
In addition to handling the above assignment and retrieval, localStorage also has the following usages:
localStorage.removeItem(); //Clear
localStorage.clear() //Clear all
localStorage.length //How many keys to get
localStorage. key() //Get the stored key content
<!DOCTYPE HTML> <html> <head> <meta charset="urf-8"/> </head> <body> <script type="text/javascript"> //判断浏览器是否支持本地存储 if(window.localStorage){ //先清除一下 localStorage.clear(); localStorage.t1="大碗干拌"; document.write(localStorage.t1); localStorage['t2']="<br/>hello word" document.write(localStorage.t2); localStorage.setItem("t3", "<br/>http://blog.csdn.net/dawanganban"); document.write(localStorage.t3); //清除t2 全部清除用clear localStorage.removeItem("t2"); for(var i=0;i<localStorage.length;i++){ document.write("<br/>" + localStorage.key(i) + "___"+localStorage.getItem(localStorage.key(i))); } }else{ alert("你的浏览器不支持"); } </script> </body> </html>
The above is the content of Xiaoqiang’s HTML5 mobile development road (19)-HTML5 Local Storage (local storage), For more related content, please pay attention to the PHP Chinese website (www.php.cn)!