JS Local Storage solution (Localstorage and UserData)

Source: Internet
Author: User
Tags sessionstorage

The rapid development of Web applications, is the local storage of some data has become an important requirement, the implementation of the program is also a lot, the most common is the cookie, we also often use, but the shortcomings of the cookie is obvious, other programs such as: IE6 above the UserData, Firefox's globalstorage, as well as Flash's local storage, has some compatibility issues in addition to flash.

Sessionstorage and Localstorage

The Web storage actually consists of two parts: Sessionstorage and Localstorage.

Sessionstorage is used to store data locally in a session, which can only be accessed by a page in the same session and destroyed when the session ends. So sessionstorage is not a persistent local store, only session-level storage.

Localstorage is used for persistent local storage, and the data is never expired unless the data is actively deleted.

UserData

Grammar:

XML <prefix:customtag id=sid style= "Behavior:url (' #default #userdata ')"/>

HTML <element style= "Behavior:url (' #default #userdata ')" Id=sid>

Scripting object. Style.behavior = "url (' #default #userdata ')"

Object. Addbehavior ("#default #userdata")

Property:

Expires sets or gets the expiration date of the UserData behavior save data.

XMLDocument gets the XML reference.

Method:

GetAttribute () Gets the specified property value.

Load (object) Loads the stored object data from the UserData store.

RemoveAttribute () Removes the specified property from the object.

Save object data is stored in an UserData store.

SetAttribute () Sets the specified property value.

Localstorage

Method:

Localstorage.getitem (Key): Gets the value that is stored locally by the specified key

Localstorage.setitem (key,value): Store value in the key field

Localstorage.removeitem (key): Delete the value stored locally by the specified key

<input id= "Localdatahook" type= "text"/><input type= "button" id= "Clearbtnhook" value= "clear"/><script Type= "Text/javascript" >//<! [Cdata[(function() {Window.localdata={hname:location.hostname? location.hostname: ' Localstatus ', IsLocalStorage:window.localStorage?true:false, DataDOM:NULL, Initdom:function(){            if(! This. DataDOM) {                Try{                     This. DataDOM = document.createelement (' input ');  This. Datadom.type = ' hidden ';  This. DataDom.style.display = "None";  This. Datadom.addbehavior (' #default #userdata '); Document.body.appendChild ( This. DataDOM); varExdate =NewDate (); Exdate= Exdate.getdate () +30;  This. Datadom.expires =exdate.toutcstring (); }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) {                returnWindow.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)} }        }    }            varText = document.getElementById (' Localdatahook '); varBTN = document.getElementById (' Clearbtnhook ')); Window.onbeforeunload=function() {Localdata.set (' Beiyuudata ', Text.value); } Btn.onclick=function() {localdata.remove (' beiyuudata '); text.value= "}; if(Localdata.get (' Beiyuudata ') ) {Text.value= Localdata.get (' Beiyuudata '); }})()
//]]></script>

JS Local Storage solution (Localstorage and UserData)

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.