Store data on the client
HTML5 provides two new ways to store data on the client:
- localstorage -Data storage with no time limit
- sessionstorage -data storage for a session
Previously, these were all done by cookies. However, cookies are not suitable for storing large amounts of data because they are passed by each request to the server, which makes the cookie slow and inefficient.
In HTML5 , the data is not passed by each server request, but only when the data is used on the request. It makes it possible to store large amounts of data without compromising site performance .
For different sites, the data is stored in different regions, and a Web site can only access its own data .
HTML5 uses JavaScript to store and access data.
Localstorage method
The data stored by the Localstorage method has no time limit. Data is still available after the second, second, or next year.
How to create and access localstorage:
<! DOCTYPE html>"text/javascript"> Localstorage.lastname="Smith";d ocument.write (" " + localstorage.lastname); </script></body>The following example counts the number of times a user accesses a page:
<! DOCTYPE Html>"Text/javascript">if(localstorage.pagecount) {Localstorage.pagecount=number (Localstorage.pagecount) +1; }Else{Localstorage.pagecount=1; }document.write ("Visits:"+ Localstorage.pagecount +"Time (s).");</script> <p> Refresh the page to see that the counter is growing. </p><p> Please close the browser window and try again, the counter will continue to count. </p></body>Sessionstorage methodThe Sessionstorage method stores data for a session. When the user closes the browser window, the data is deleted.
How to create and access a sessionstorage:
<! DOCTYPE html>"text/javascript"> Sessionstorage.lastname="Smith";d ocument.write (sessionstorage.lastname); </script></body>
The following example counts the number of times a user accesses a page in the current session:
<! DOCTYPE Html>"Text/javascript">if(sessionstorage.pagecount) {Sessionstorage.pagecount=number (Sessionstorage.pagecount) +1; }Else{Sessionstorage.pagecount=1; }document.write ("Visits"+ Sessionstorage.pagecount +"Time (s) this session.");</script> <p> Refresh the page to see that the counter is growing. </p><p> Please close the browser window and try again, the counter has been reset. </p></body>HTML 5 Web Storage