Localstorage compatible IE6/7 with Addbehavior implementation

Source: Internet
Author: User

Production process I will not say, the program will be taken up under

Definefunction(){    if(' Localstorage 'inchWindowreturn; functionStorage () { This. Box = Document.body | | Document.getelementbytagname (' head ') [0] | |document.documentelement;  This. Name = ' Localstorage ' This. data = Document.createelement ( This. Name);  This. Data.addbehavior ("#default #userdata");  This. Box.appendchild ( This. Data);  This. Map = [];  This. length = This. Length (); } Storage.prototype.setItem=function(name,val) {if(name== ' Localstorage-map '){            Throw NewError ("This was localstorage in key [Localstorage-map] not use!")            return ; }        if( This. map.length==0){             This. Data.load (' Localstorage-map '); vardata = This. Data.getattribute (' Localstorage-map '); if(data!=NULL){                 This. Map = Data.split (', '); }        }        varFlag =true;  for(varIinch  This. Map) {            if( This. map[i] = =name) {Flag=false; }        }        if(flag) { This. Map.push (name)} This. Data.setattribute (Name,val); varDate =NewDate (); Date.setdate (Date.getdate ()+700);  This. Data.expires =date.toutcstring ();  This. Data.save (name);  This. Data.setattribute (' Localstorage-map ', This. map);  This. Data.save (' Localstorage-map '); } Storage.prototype.getItem=function(name) {if(name = = ' Localstorage-map ')){            Throw NewError ("This was localstorage in key [Localstorage-map] not use!"); return; }         This. Data.load (name); return  This. Data.getattribute (name);    }; Storage.prototype.length=function(){        if( This. map.length==0){             This. Data.load (' Localstorage-map '); vardata = This. Data.getattribute (' Localstorage-map '); if(data!=NULL){                 This. Map = Data.split (', '); }        }         for(vari = This. map.length-1; I >= 0; i--) {alert ( This. GetItem ( This. Map[i])) if( This. GetItem ( This. map[i]) ==undefined | | This. GetItem ( This. map[i]) = = ""){                 This. Map.splice (i,1); }        }        return  This. Map.length;    }; Storage.prototype.removeItem=function(name) {if(typeofname== "Undefined" | | name== "")return; if( This. map.length==0){            if( This. GetItem (' Localstorage-map ')! =NULL){                 This. Map = This. GetItem (' Localstorage-map '). Split (', '); }        }         for(varIinch  This. Map) {            if( This. map[i] = =name) {                 This. Map.splice (i,1); }        }         This. Data.load (name);  This. Data.setattribute (name,undefined);  This. Data.save (name); return true; }, Storage.prototype.clear=function(){        if( This. map.length==0){            if( This. GetItem (' Localstorage-map ')! =NULL){                 This. Map = This. GetItem (' Localstorage-map '). Split (', '); }        }         for(varIinch  This. Map) {             This. RemoveItem ( This. Map[i]); }} window.localstorage=NewStorage ();});

Calling programs

Localstorage.setitem (' value ', ' {askdjf:ddd} ')            localstorage.setitem (' name ', ' {ddddddd:ddd} ') Alert (            localstorage.getitem (' value ')) alert (            localstorage.getitem (' name '))            alert ( Localstorage.removeitem (' name '))            alert (localstorage.length);

I was just a simple test to be able, do not know where there are problems, looking at the twos!!!!!

Localstorage compatible IE6/7 with Addbehavior implementation

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.