Example of localStorage data cached by the browser

Source: Internet
Author: User
Tags json browser cache

The actual application returns the loss of data read by ajax in the previous step .. You can save it through the browser cache.

The code is as follows: Copy code

Function saveCity (){
 
Var province = $ ("# province"). val ();
Var city = $ ("# city"). val ();
Var area = $ ("# area"). val ();
// Alert ("syheng" + province + "chis" + city + "ara" + area );
/* LocalStorage. removeItem ("province ");
LocalStorage. removeItem ("city ");
LocalStorage. removeItem ("area ");*/
LocalStorage. setItem ('Province ', province );
LocalStorage. setItem ('city', city );
LocalStorage. setItem ('region', area );
  }

Function loadCity (){
Var province = localStorage. getItem ('Province ');
Var city = localStorage. getItem ('city ');
Var area = localStorage. getItem ('region ');
// Alert ("syheng" + province + "chis" + city + "ara" + area );
If (province! = Null & city! = Null & area! = Null ){
$. Ajax ({
Url: baseUrl + '/getAddressJson. Json ',
Data :{
'Region': area,
'City': city,
'Province ': province
},
Type: 'post', // data transmission method
Async: false,
DataType: "json ",
Success: function (data ){
$ ("# Province"). empty ();
$ ("# City"). empty ();
$ ("# Area"). empty ();
$. Each (data. plist, function (index, content)
               { 
Var appendStr = "<option value = '" + content. code + "'";
If (content. code = province ){
AppendStr = appendStr + "selected = 'selected '";
         }
AppendStr = appendStr + ">" + content. name + "</option>"
$ ("# Province"). append (appendStr );
                  
});
            
$. Each (data. clist, function (index, content)
               { 
Var appendStr = "<option value = '" + content. code + "'";
If (content. code = city ){
AppendStr = appendStr + "selected = 'selected '";
         }
AppendStr = appendStr + ">" + content. name + "</option>"
$ ("# City"). append (appendStr );
                  
});
$. Each (data. alist, function (index, content)
               { 
Var appendStr = "<option value = '" + content. code + "'";
If (content. code = area ){
AppendStr = appendStr + "selected = 'selected '";
         }
AppendStr = appendStr + ">" + content. name + "</option>"
$ ("# Area"). append (appendStr );
                  
});
          }
});
  }
 
 
   
 }
LoadCity ();


The following is an explanation


① The localstorage size is limited to about 5 million characters. Different browsers are inconsistent.
② Localstorage cannot be read in private mode
③ Localstorage is essentially a file to be read and written. If there is too much data, it will be relatively slow (firefox will import the data into the memory at a time, and it will be scary to think about it)
④ Localstorage cannot be crawled by Crawlers. Do not use it to completely replace URL parameter passing.
Localstorage storage objects can be divided into two types:

① SessionStrage: session refers to the period from when a user browses a website and closes the website, the validity period of the session object is only so long.

② LocalStorage: save the data on the client hardware device, no matter what it is, that is, the data is still in the next time you open the computer.

The difference between the two is temporary storage and long-term storage.

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.