Js local storage solution-localStorage and userData

Source: Internet
Author: User
With the rapid development of WEB applications, local storage of some data has become an important requirement, and there are many implementation solutions. The most common one is cookie, which is frequently used, however, the disadvantage of cookie is obvious. Other solutions...

With the rapid development of WEB applications, local storage of some data has become an important requirement, and there are many implementation solutions. The most common one is cookie, which is frequently used, however, the disadvantage of cookie is obvious. Other solutions, such as userData of IE6 or above, globalStorage of Firefox, and local Flash storage, apart from Flash, the others have some compatibility issues.

SessionStorage and localStorage

Web Storage consists of sessionStorage and localStorage.

SessionStorage is used to locally store data in a session. The data can be accessed only on pages in the same session. When the session ends, the data is also destroyed. SessionStorage is not a persistent local storage, but a session-level storage.

LocalStorage is used for persistent local storage. Unless data is actively deleted, the data will never expire.

UserData

Syntax:

XML  
 HTML  
 
  Scripting    object .style.behavior = "url('#default#userData')"object.addBehavior ("#default#userData")
 

Attribute:

  • Expires sets or obtains the expiration date of the data saved by userData behavior.

  • XMLDocument gets the reference of XML.

Method:

  • GetAttribute () gets the specified property value.

  • Load (object) loads the stored object data from the userData storage area.

  • RemoveAttribute () removes the specified property of an object.

  • Save (object) stores the object data in a userData storage area.

  • SetAttribute () sets the specified attribute value.

LocalStorage

Method:

  • LocalStorage. getItem (key): Get the value of the local storage of the specified key

  • LocalStorage. setItem (key, value): store the value to the key field

  • LocalStorage. removeItem (key): deletes the value of the local storage of the specified key.

Encapsulation
LocalData = {hname: location. hostname? Location. hostname: 'localstatus', isLocalStorage: window. localStorage? True: false, dataDom: null, initDom: function () {// initialize userData if (! This. dataDom) {try {this. dataDom = document. createElement ('input'); // use the hidden input element this. dataDom. type = 'ddn'; this. dataDom. style. display = "none"; this. dataDom. addBehavior ('# default # userdata'); // This is the syntax document of userData. body. appendChild (this. dataDom); var exDate = new Date (); exDate = exDate. getDate () + 30; this. dataDom. expires = exDate. toUTCString (); // set the expiration time} catch (ex) {return false ;}} return true ;}, set: function (key, value) {if (this. isLocalStorage) {window. localStorage. setItem (key, value);} else {if (this. initDom () {this. dataDom. load (this. hname); this. dataDom. setAttribute (key, value); this. dataDom. save (this. hname) }}, get: function (key) {if (this. isLocalStorage) {return window. localStorage. getItem (key);} else {if (this. initDom () {this. dataDom. load (this. hname); return this. dataDom. getAttribute (key) ;}}, remove: function (key) {if (this. isLocalStorage) {localStorage. removeItem (key);} else {if (this. initDom () {this. dataDom. load (this. hname); this. dataDom. removeAttribute (key); this. dataDom. save (this. hname )}}}}

The above is the js local storage solution-content of localStorage and userData. For more information, see PHP Chinese Network (www.php1.cn )!

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.