Currently the best storage localstorage

Source: Internet
Author: User

Read this article and you can learn the following.

1.What is the use of localstorage?

2.general usage of localstorage and how to store images.

First of all, what is localstorage ?

It is a new storage technology for HTML5. But it can only store strings. This previous part of the work has been done by a cookie. However, cookies can store only about 5k of data. and Localstorage can store 5m!! This is very exciting both on the PC side and on the mobile side.

Since there is such a slight understanding. We'll probably see what it's used for.

These two days I just did a mobile project, the page is not content, but there are two big pictures. (The page is more than 200 KB) in order to improve the page loading effect, I am very eager to try the next localstorage to cache the picture.

But since Localstorage can only store strings, what's the way to make it store a picture?

At this point we can think of the format of the image can be converted, we can use canvas to do this. Next, we look at the code.

We write a function like this, we just need to define the initial img for the SRC to take the src path string as the second parameter passed into the function.

So we can set each localstorage key.

And then if we're going to take out this picture. Then we'll just use the localstorage.getitem (' key ') API to take out a bunch of base64 characters. Given to SRC, or to the URL of background. So the next time you open the page is to take out the picture directly. Isn't it cool?!

We can look at the effect of this. This also solves the F12 I used to see other people's website.

Currently the best storage localstorage

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.