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:
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 )!