HTML5 Mobile Development Practical knowledge-local storage (1)

Source: Internet
Author: User

The local cache is a new technology that HTML5, and the advent of this technology has made it possible to develop mobile web. As we all know, speed is key to building a high-performance mobile application. Before HTML5, only cookies were able to store data, only 4kb in size. This severely limits the storage of application files, which can lead to a longer load time for web-developed mobile applications. With local storage, web mobile apps can be closer to native.

In the browser, local storage is called through Window.localstorage. The code that determines 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 are going to store the data locally, the simplest way is for window. Localstorage adds a property and assigns a value to it. For example, if we want to store a data name whose value is Tom, it can be implemented in the following way:

Window.localStorage.name = "Tom"

It is important to note that string variables require quotation marks. When we want to take out the data city in local storage, we can use the GetItem method. The entire code flow is as follows:

var storage = Window.localstorage;storage.name = "Tom";//Remove the name data var name1 = Storage.getitem ("name"); alert (name1);

The result of this code in the Chrome browser console is a prompt to show Tom. It can be seen that we have correctly stored and read the data in this way.

If we want to delete these stored data, we can use the RemoveItem method. Add the following code to the above code:

Storage.removeitem ("name");

At this point we will show null when we alert again, because this data has been emptied.

HTML5 Mobile Development Practical knowledge-local storage (1)

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.