Summary client data storage

Source: Internet
Author: User
Tags sessionstorage

Summary

What is client data storage?

Simply put, data is stored on a specific client, reducing the number of requests to the server.

Why is client data storage required?

In the response time, the time taken by the http request response cannot be underestimated. Therefore, reducing http requests can improve user experience and reduce the chance of exceptions.

How to use client data storage?

In my limited knowledge, client data storage is divided into two types: access to data on the same page, and access to cross-page data storage.

Same page Data Access

I know the same page data storage has five types (the script library uses jquery-1.6 ):

The first method is to useAccess Data

Take $ ("# cds1"). val () Storage: $ ("# cds1"). val ('other data ').

The second is to use html custom attributes to access data.

 

Take $ ("# cds2"). attr ("val") Storage: $ ("# cds2"). attr ("val", "other data ");

The third method is to use the newly added attribute "data-youvalue" in html5.

 

$ ("# Cds3 "). data ("val") Storage: $ ("# cds3 "). data ("val", "other val"), note that if data-val has an initial value, $ ("# cds3 "). data ("val") will lose the previous 0. You can use $ ("# cds3 "). attr ("data-val") solves this problem. but if it is through $ ("# cds3 "). set data ("val", "001") and then use $ ("# cds3 "). data ("val") is not lost.

The fourth method is to add

 

The Code is as follows (the Code passes the test in ie [7 | 8 | 9] and an error is reported in ff4.0.1 ):

 
 
  1. // set data  
  2. var dS = document.getElementById("cds4");  
  3. dS.setAttribute("name", "chen qi liang");  
  4. dS.save("info");  
  5. // get data  
  6. dS.load("info");  
  7. alert(dS.getAttribute("name"));  
  8. // delete data  
  9. dS.removeAttribute("name");  
  10. dS.save("info"); 

The fifth method is to use the sessionStorage Code as follows (ie [7 | 8 | 9], ff4.0.1 passed the test ):

 
 
  1. SessionStorage. name = "chen qi liang"; or sessionStorage. setItem ("name", "chen qi liang ")
  2. Alert (sessionStorage. name); or alert (sessionStorage. getItem ("name "));

Cross-page Data Access

I know three methods.

The first method is url parameter transfer (in my opinion, client data access is not strictly valid)

 
 
  1. Url? Parameter = value
  2. Var cql = {
  3. // Get querystring, ignore the case sensitive
  4. // If no match then return ""
  5. // This method is provided by colleague fan Zhanfang
  6. GetQueryStr: function (param ){
  7. Var re = new RegExp ("[&,?] "+ Param +" = ([^ \ &] *) "," I ");
  8. Var a = re.exe c (document. location. search );
  9. If (a = null)
  10. Return "";
  11. Return a [1];
  12. }
  13. };

The second method is cookie-based access. If I have written a plug-in, refer to response. Set the following in c:

 
 
  1. HttpCookie cookie = new HttpCookie ("test ");
  2. Cookie. HttpOnly = true; // http-specific cookie
  3. Cookie. Values ["item1"] = "value1 ";
  4. Cookie. Values ["item2"] = "value2 ";
  5. HttpContext. Current. Response. Cookies. Set (cookie );

The third method is globalStorage (replaced by localStorage in html5 ):

 
 
  1. GlobalStorage ['wrox. com']. name = 'wrox'; // Save the data var name = globalStorage ['wrox. com']. name; // obtain the data
  2. LocalStorage. setItem ("name", "chemdmeo"); // save data
  3. LocalStorage. book = 'js'; // save data
  4. LocalStorage. book // get data
  5. LocalStorage. getItem ("name") // obtain data
  6. // Compatible only supports globalStorage's browser function getLocalStorage () {if (typeof localStorage = 'object') {return localStorage;} else if (typeof globalStorage = 'object ') {return globalStorage [location. host];} else {throw new Error ('local storage not available. ');}};

Note: not all browsers support the preceding dom storage.

It is insecure for the client to store data. This method is not recommended for storing sensitive data.

Recommended by editors]

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.