HTML BASIC
HTML Introduction
HTML Getting Started
HTML Elements
HTML Attributes
HTML Headings
HTML Paragraphs
HTML Links
HTML Text Formatting
HTML Styles
HTML Images
HTML Tables
HTML Lists
HTML Forms
HTML Iframes
HTML ADVANCED
HTML Doctypes
HTML Layout
HTML Head
HTML Meta
HTML Scripts
HTML Entities
HTML URL
HTML URL Encode
HTML Validation
HTML5 FEATURES
HTML5 New Input Types
HTML5 Canvas
HTML5 SVG
HTML5 Audio
HTML5 Video
HTML5 Web Storage
HTML5 Application Cache
HTML5 Web Workers
HTML5 SSE
HTML5 Geolocation
HTML5 Drag & Drop
HTML5 Web Storage - Html5 基础教程 - IT书包
网站首页
HTML5 Web Storage
### What is Web Storage? The HTML5's web storage feature lets you store some information locally on the user's computer, similar to cookies, but it is faster and much better than cookies. However, web storage is no more secure than cookies. Please check out the tutorial on PHP cookies to learn more about cookies. The information stored in the web storage isn't sent to the web server as opposed to the cookies where data sent to the server with every request. Also, where cookies let you store a small amount of data (nearly 4KB), the web storage allows you to store up to 5MB of data. There are two types of web storage, which differ in scope and lifetime: - **Local storage** — The local storage uses the localStorage object to store data for your entire website on a permanent basis. That means the stored local data will be available on the next day, the next week, or the next year unless you remove it. - **Session storage** — The session storage uses the sessionStorage object to store data on a temporary basis, for a single browser window or tab. The data disappears when session ends i.e. when the user closes that browser window or tab. > Tip: The HTML5's web storage feature is supported in all major modern web browsers like Firefox, Chrome, Opera, Safari and Internet Explorer 8 and above. ### The localStorage Object As stated earlier, the localStorage object stores the data with no expiration date. Each piece of data is stored in a key/value pair. The key identifies the name of the information (like 'first_name'), and the value is the value associated with that key (say 'Peter'). Here's an example: ```javascript <script> // Check if the localStorage object exists if(localStorage) { // Store data localStorage.setItem("first_name", "Peter"); // Retrieve data alert("Hi, " + localStorage.getItem("first_name")); } else { alert("Sorry, your browser do not support local storage."); } </script> ``` ### Example explained: The above JavaScript code has the following meaning: - **localStorage.setItem(key, value)** stores the value associated with a key. - **localStorage.getItem(key)** retrieves the value associated with the key. You can also remove a particular item from the storage if it exists, by passing the key name to the `removeItem()` method, like `localStorage.removeItem("first_name")`. However, if you want to remove the complete storage use the `clear()` method, like l`ocalStorage.clear()`. The `clear()` method takes no arguments, and simply clears all key/value pairs from localStorage at once, so think carefully before you using it. > Note: The web storage data (both localStorage and sessionStorage) will not be available between different browsers, for example the data stored in Firefox browser will not available in Google Chrome, Safari, Internet Explorer or other browsers. ### The sessionStorage Object The `sessionStorage` object work in the same way as `localStorage`, except that it stores the data only for one session i.e. the data remains until the user closes that window or tab. Let's try out the following example to understand how it basically works: ```javascript <script> // Check if the sessionStorage object exists if(sessionStorage) { // Store data sessionStorage.setItem("last_name", "Parker"); // Retrieve data alert("Hi, " + localStorage.getItem("first_name") + " " + sessionStorage.getItem("last_name")); } else { alert("Sorry, your browser do not support session storage."); } </script> ```
上一篇:
HTML5 Video
下一篇:
HTML5 Application Cache