The difference and usage of cookie/sessionstorage/localstorage

Source: Internet
Author: User
Tags setcookie sessionstorage
difference betweenLocal cookies can be used across browsers, and can be set for expiration, and can be used across labels. Sessionstorage only for single page storage data localstorage used to permanently save data same Point

are used to store data. Usage Cookies

JS Operation Cookie Method! 
    Write cookies function Setcookie (name,value) {var days = 30; 
    var exp = new Date (); 
    Exp.settime (Exp.gettime () + days*24*60*60*1000); 
Document.cookie = name + "=" + Escape (value) + "expires=" + exp.togmtstring (); //Read cookies function GetCookie (name) {var arr,reg=new RegExp ("(^|)" +name+ "= ([^;]

    *)(;|$)"); 
    if (Arr=document.cookie.match (reg)) return unescape (arr[2]); 
else return null; 
    //Delete the cookie function Delcookie (name) {var exp = new Date (); 
    Exp.settime (Exp.gettime ()-1); 
    var cval=getcookie (name); 
if (cval!=null) document.cookie= name + "=" +cval+ "; expires=" +exp.togmtstring (); 
//Use sample Setcookie ("name", "Hayden"); 

Alert (GetCookie ("name")); 


If you need to set the custom expiration//Then replace the above Setcookie function with the following two functions on OK; 
    Program code function Setcookie (name,value,time) {var strsec = getsec (time); 
    var exp = new Date (); 
    Exp.settime (Exp.gettime () + strsec*1); Document.cookie = name + "="+ Escape (value) +"; expires= "+ exp.togmtstring (); 
   function Getsec (str) {alert (str); 
   var str1=str.substring (1,str.length) *1; 
   var str2=str.substring (0,1); 
   if (str2== "s") {return str1*1000; 
   else if (str2== "H") {return str1*60*60*1000; 
   else if (str2== "D") {return str1*24*60*60*1000; }//This is an example of a set expiration time://S20 is representative of 20 seconds//h refers to hours, such as 12 hours is: H12//d is the number of days, 30 days: D30 Setcookie ("name", "Hayden", "S20");
Sessionstorage and Localstorage
To determine whether the browser supports Localstorage, you can use the following code:

if (window.localstorage) { 
    alert ("Browse support Localstorage") 
 }else{    
    Alert ("Browse temporarily does not support Localstorage") 
  } 
  //or 
if (typeof window.localstorage = = ' undefined ') {     
  alert (" Browse temporarily does not support Localstorage ") 
}
Sessionstorage.setitem (" Key "," value ");     Localstorage.setitem ("Site", "js8.in");

Sessionstorage.getitem ("key");  Localstorage.getitem ("site");

Sessionstorage.removeitem ("key");   Localstorage.removeitem ("site");
Remove all
sessionstorage.clear ();
Localstorage.clear ();

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.