Detailed description of HTML5 local storage, html5localstorage
Speaking of local storage, this is a tough task to go To the HTML5 step, as shown in the previous history:
The earliest Cookies are naturally known to all. The problem is that they are too small and approximately 4 kb. IE6 only supports 20 cookies for each domain name, which is too small. The advantage is that everyone supports it, and the support is quite good. A long time ago, users who disabled cookies gradually did not exist, as if they had previously disabled javascript.
UserData is Internet Explorer. Flash is the most commonly used, and the space is 25 times that of cookies, which is basically enough. After that, Google launched Gears. Although there are no restrictions, it is uncomfortable to install additional plug-ins (no specific research has been conducted ). By HTML5, these are all unified. The official suggestion is that each website is 5 MB. If it is very large, some strings will be saved, which is enough. The strange thing is that all supported browsers currently use 5 MB. Although some browsers can be set for users, for web page makers, it is appropriate to consider the current situation in 5 MB.
For example, IE was supported in 8.0, which is quite unexpected. However, during IE and Firefox testing, you must upload the file to the server (or localhost) and directly open the local HTML file.
First, check whether the browser supports local storage. In HTML5, local storage is a window attribute, including localStorage and sessionStorage. The difference between the two can be clearly identified by name. The former is always local, the latter is only accompanied by the session. Once the window is closed, it will be gone. The two are used in the same way. Here, localStorage is used as an example.
Copy the content to the clipboard using JavaScript Code
- If (window. localStorage ){
- Alert ('this browser supports localstore ');
- } Else {
- Alert ('this browser does NOT support localstore ');
- }
You can directly add an attribute to window. localStorage, such as window. localStorage. a or window. localStorage ["a"]. The method for reading, writing, and deleting data is simple. It exists as a key-value pair, as shown below:
Copy the content to the clipboard using JavaScript Code
- LocalStorage. a = 3; // set a to "3"
- LocalStorage ["a"] = "sfsf"; // set a to "sfsf" to overwrite the above value
- LocalStorage. setItem ("B", "isaac"); // Set B to "isaac"
- Var a1 = localStorage ["a"]; // obtain the value of
- Var a2 = localStorage. a; // obtain the value of
- Var B = localStorage. getItem ("B"); // obtain the value of B
- LocalStorage. removeItem ("c"); // clear the value of c
Here, we recommend that you use getItem () and setItem () to clear key-value pairs and use removeItem (). To clear all key-value pairs at a time, use clear (). In addition, HTML5 provides a key () method, which can be used when you do not know which key values are available, as follows:
Copy the content to the clipboard using JavaScript Code
- Var storage = window. localStorage;
- Function showStorage (){
- For (var I = 0; I <storage. length; I ++ ){
- // Key (I) obtains the corresponding key, and obtains the corresponding value using the getItem () method.
- Document. write (storage. key (I) + ":" + storage. getItem (storage. key (I) + "<br> ");
- }
- }
Write a simple counter that uses local storage:
Copy the content to the clipboard using JavaScript Code
- Var storage = window. localStorage;
- If (! Storage. getItem ("pageLoadCount") storage. setItem ("pageLoadCount", 0 );
- Storage. pageLoadCount = parseInt (storage. getItem ("pageLoadCount") + 1; // format conversion required
- Document. getElementByIdx_x ("count"). innerHTML = storage. pageLoadCount;
- ShowStorage ();
As shown in:
Note that the local storage of HTML5 can only store strings, and will be automatically converted to strings during storage of any format. Therefore, you need to convert the data type during reading. This is why parseInt must be used in the previous code.
In addition, sometimes when setItem () is set on the iPhone/iPad, a strange QUOTA_EXCEEDED_ERR error occurs. In this case, removeItem () is OK before setItem.
HTML5 local storage also provides a storage event that can be used to listen for changes to key-value pairs as follows:
Copy the content to the clipboard using JavaScript Code
- If (window. addEventListener ){
- Window. addEventListener ("storage", handle_storage, false );
- } Else if (window. attachEvent ){
- Window. attachEvent ("onstorage", handle_storage );
- }
- Function handle_storage (e ){
- If (! E) {e = window. event ;}
- // ShowStorage ();
- }
The event variable e is a StorageEvent object and provides some practical attributes to observe the changes of key-value pairs, as shown in the following table:
Property |
Type |
Description |
Key |
String |
The named key that was added, removed, or moddified |
OldValue |
Any |
The previous value (now overwritten), or null if a new item was added |
NewValue |
Any |
The new value, or null if an item was added |
Url/uri |
String |
The page that called the method that triggered this change |
Add two key-value pairs a and B and add a button. Set a fixed value for a. When you click the button, modify the value of B:
Copy the content to the clipboard using JavaScript Code
- <Body>
- <P> You have viewed this page <span id = "count"> 0 </span> time (s). </p>
- <P> <input type = "button" value = "changeStorage" onClick = "changeS ()"/> </p>
- <Script>
- Var storage = window. localStorage;
- If (! Storage. getItem ("pageLoadCount") storage. setItem ("pageLoadCount", 0 );
- Storage. pageLoadCount = parseInt (storage. getItem ("pageLoadCount") + 1; // format conversion required
- Document. getElementByIdx_x ("count"). innerHTML = storage. pageLoadCount;
- ShowStorage ();
- If (window. addEventListener ){
- Window. addEventListener ("storage", handle_storage, false );
- } Elseif (window. attachEvent ){
- Window. attachEvent ("onstorage", handle_storage );
- }
- Function handle_storage (e ){
- If (! E) {e = window. event ;}
- ShowObject (e );
- }
- Function showObject (obj ){
- // Recursively display objects
- If (! Obj) {return ;}
- For (var I in obj ){
- If (typeof (obj [I])! = "Object" | obj [I] = null ){
- Document. write (I + ":" + obj [I] + "<br/> ");
- } Else {
- Document. write (I + ": object" + "<br/> ");
- }
- }
- }
- Storage. setItem ("a", 5 );
- Function changeS (){
- // Modify a key value to test the storage event
- If (! Storage. getItem ("B") {storage. setItem ("B", 0 );}
- Storage. setItem ('B', parseInt (storage. getItem ('B') + 1 );
- }
- Function showStorage (){
- // Display the key-value pairs in localStorage cyclically
- For (var I = 0; I <storage. length; I ++ ){
- // Key (I) obtains the corresponding key, and obtains the corresponding value using the getItem () method.
- Document. write (storage. key (I) + ":" + storage. getItem (storage. key (I) + "<br> ");
- }
- }
- </Script>
- </Body>
The test shows that the browser currently does not support this function very well. It only supports iPad and Firefox. In addition, Firefox does not support these attributes at all. The iPad supports very well. It uses e. uri (not e. url) and Safari on the desktop.
Currently, browsers have good developer debugging functions. The following are debugging tools for Chrome and Firefox to view LocalStorage:
In addition, javascript currently uses a lot of json format. If you want to store it locally, you can directly call JSON. stringify () to convert it into a string. After reading the data, call JSON. parse () to convert the string to json format, as shown below:
Copy the content to the clipboard using JavaScript Code
- Var details = {author: "isaac", "description": "fresheggs", "rating": 100 };
- Storage. setItem ("details", JSON. stringify (details ));
- Details = JSON. parse (storage. getItem ("details "));
JSON objects are basically supported in browsers that support localStorage. It should be noted that IE8 supports JSON, but if the following compatible mode code is added, it cannot be switched to IE7 mode (localStorage is still supported at this time, although the window is displayed. localStorage is [object] instead of the previous [object Storage], but it is found that getItem (), setItem () and so on can be used ).
Copy the content to the clipboard using JavaScript Code
- <Meta content = "IE = 7" http-equiv = "X-UA-Compatible"/>
The above is a detailed description of all the content of the HTML5 local storage LocalStorage provided by xiaobian. I hope you can provide more support to the customer's home ~
Address: http://www.cnblogs.com/androidshouce/archive/2016/06/24/5613033.html