JavaScript local storage cookies, localstorage

Source: Internet
Author: User
Tags setcookie sessionstorage

First, the local storage of cookies

The first is the usual cookie method, which has a lot of relevant code and W3cschool cookieson the internet.

1 //Storing Cookies2 functionSetcookie (name,value,expiredays)3 { 4     varargv =setcookie.arguments;5     varARGC =setCookie.arguments.length;6     varLargeexpdate =NewDate ();7     varexpires = (argc > 2)? Argv[2]: expiredays; 8     if(expires!=NULL) 9     { TenLargeexpdate.settime (Largeexpdate.gettime () + (expires*1000*3600*24));  One     }  ADocument.cookie = name + "=" + Escape (value) + ((expires = =NULL) ? "" : ("; Expires= "+largeexpdate.togmtstring ()));  - } -  the //Access to Cookies - functionGetCookie (Name) - {  -     varSearch = Name + "=" +     if(Document.cookie.length > 0)  -     {  +offset =document.cookie.indexOf (search) A         if(Offset! =-1)  at         {  -Offset + =Search.length -End = Document.cookie.indexOf (";", offset) -             if(end = =-1) end =Document.cookie.length -             returnunescape (document.cookie.substring (offset, end)) -         }  in         Else return"" -     }  to }  +  - //Delete Cookies the functionDeletecookie (name) * {  $     varExpdate =NewDate ();Panax NotoginsengExpdate.settime (Expdate.gettime ()-(86400 * 1000 * 1));  -Setcookie (Name, "", expdate);  the}

Call directly on the line when you use it.

// store setcookie (' name ', value, expiration time); // expiration time may not be written // get GetCookie (' name ')// Delete deletecookie (' name ');
Find ZijianConsider when you need to store multiple values in a single cookie, you can deposit multiple values in the sub-health.
Setcookie (' Openif2 ', "k1=1&k2=2&k3=3"); // When you obtain cookies, you can obtain each individual child's health var child = GetCookie (' openif2 ')var childs = Child.split (' & ');  for (var i = 0;i<childs.length;i++) {    console.log (childs[i])}

attached (note 8 hours time difference): Local Date :ToString () Local Date :toLocaleString () GMT World Time :toGMTString () UTC World Time :toUTCString ()

Second,HTML5 local storage

H5 Web Storage

Support Detection:

if (window.localstorage) {    //  support local Storage }
LocalstorageLocalstorage saves data permanently and does not disappear as the browser closes. Store by domain name, does not conflict with other domain name key-value pairs storage: key/value

Localstorage API

1.setItem (key, value), save or set data. If key already exists, overwrite the value corresponding to key and add key and value if it does not exist.

2.key (index); Gets the key that specifies the location of the subscript

3.length Get localstorage How many data are there altogether?

The method of traversing Localstorage data can be implemented with the key (index) method

4.clear (); Empty all localstorage data under the same domain name

5.storage Event: This event is triggered when a homologous localstorage or sessionstorage has changed.

6.removeItem (): delete data, delete the corresponding value by key

SessionstorageSessionstorage saves data temporarily and disappears when the page is closed. Everything else is the same as localstorage. Sessionstorage cannot be accessed across pages, nor does it trigger storage events across tabs. It is only limited to the current tab. Note: the local cache added using the H5 method is not in cookies, but in localstorage or sessionstorage . The above is a personal summary of some of the local storage information, I hope to give the students need some help. Have a question or have a better suggestion, welcome to come to discuss!

JavaScript local storage cookies, 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.