Differences between localstorage and sessionstorage

Source: Internet
Author: User
Tags sessionstorage

// Result of the chrome test;

Knowledge Point 1: The difference between localstorage and sessionstorage; localstorage life cycle is permanent, which means that the information will always exist unless the user displays the localstorage information on the UI provided by the browser. The sessionstorage lifecycle is the current window or tab. Once the window or tab is permanently closed, all data stored through sessionstorage is cleared.

Localstorage method: localstorage. clear (); localstorage. getitem (); localstorage. key (); localstorage. setitem (); localstorage. removeitem (); sessionstorage has an additional length on the localstorage method. Note that localstorage provides events;
    if(window.addEventListener){        window.addEventListener("storage",handle_storage,false);     }else if(window.attachEvent){             window.attachEvent("onstorage",handle_storage);     };     function handle_storage(e){        e = e || window.e;        console.log(e);         };

 

/* Note: The storage event trigger seems a bit problematic. The storage event of the window is not triggered after the setitem is performed on the page, but the storage events of a.htmland B .html are generated at the same time, setitem on page a triggers the storage event of window on page B, and setitem on page B triggers the storage event of window on page. in ie9, the setting value of the page can trigger the storage event of the current page, and the setting value of the current page can also trigger the storage event of other page windows under the same "origin, this seems more appealing. read more: http://www.cnblogs.com/shihao/archive/2011/12/23/2298854.html * // so to trigger localstorage for the current page to use custom events, we need to encapsulate one ourselves;
VaR storage = {setitem: function (K, v) {var Se = document. createevent ("storageevent"); se. initstorageevent ('store', false, false, K, localstorage. getitem (K), V, "", ""); window. dispatchevent (SE); localstorage. setitem (K, v) ;}, removeitem: function (k) {localstorage. removeitem (k); var Se = document. createevent ("storageevent"); se. initstorageevent ('store', false, false, K, localstorage. getitem (K), V, "", ""); window. dispatchevent (SE) ;}}; // use the Custom Event window. addeventlistener ("Storage", function (e) {console. log (e) ;}, false );
    //Read More : http://www.cnblogs.com/cczw/p/3196195.html    

Differences between localstorage and sessionstorage

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.