Knowledge required for HTML5 mobile development-Local Storage (1)

Source: Internet
Author: User

Knowledge required for HTML5 mobile development-Local Storage (1)

Local cache is a new HTML5 technology, which makes mobile web development possible. We all know that speed is the key to creating a high-performance mobile application. Before HTML5, only cookies can store data, with a size of only 4 kb. This severely limits the storage of Application Files, resulting in a long loading time for mobile applications developed by the web. With local storage, web mobile applications can be closer to the native.

In the browser, local storage is called through window. localStorage. The code used to determine whether the browser supports local storage is as follows:

if(window.localStorage){ alert('This browser supports localStorage');}else{ alert('This browser does NOT supportlocalStorage');}

If we want to store data locally, the simplest method is window. Add an attribute to localStorage and assign values to it. For example, if we want to store a data name whose value is Tom, we can achieve this through the following method:

window.localStorage.name = “Tom”

Note that string variables must be enclosed in quotation marks. When we want to retrieve the data market in the local storage, we can use the getItem method. The entire code process is as follows:

Var storage = window. localStorage; storage. name = "Tom"; // retrieve name Data var name1 = storage. getItem ("name"); alert (name1 );

The result displayed in the Chrome console is a prompt box showing Tom. It can be seen that we have properly stored and read data in this way.

If you want to delete the stored data, you can use the removeItem method. Add the following code to the above Code:

storage.removeItem(“name”);

At this time, when alert is used again, null is displayed because the data has been cleared.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.