Home > Web Front-end > JS Tutorial > body text

javascript globalStorage class code_javascript skills

WBOY
Release: 2016-05-16 18:51:46
Original
1195 people have browsed it
globalStorage
This is also proposed in HTML5. After the browser is closed, the information stored using globalStorage can still be retained, and the storage capacity is much larger than IE's userdata. One domain is 5120k. Like sessionStorage, information stored on any page in the domain can be shared by all pages.
Scope
globalStorage['z.baidu.com'] All pages under z.baidu.com can use this space
globalStorage['baidu.com'] All pages under baidu.com All pages can use this space
globalStorage['com']: All com domain names can share this space
globalStorage['']: All pages can use this space
Now Firefox only Supports globalStorage storage under the current domain. If the public domain is used, an error similar to "Security error" code: "1000" will result.
Expiration time
According to the description of HTML5, globalStorage will only expire when there is a security issue or when the user requests it. Browsers should avoid deleting data that is being accessed by scripts, and userdata should be user-writable.
Therefore, our script must be able to control the expiration time. We can store the expiration time in a certain area of ​​globalStorage and determine whether it has expired when loading. This can solve the problem of expiration time to a certain extent.
When storing, the expiration time is also stored
The above is the information I found online. In order to be compatible with non-IE browser "userdata", I improved the
"userdata" I wrote myself (see UserData usage summary), it is now compatible with IE and a browser that supports globalStorage.
Copy code The code is as follows:

function behaviorUserdata(udObj)
{
var me = this;
if(CMInfo.Bs_Name=='IE') //Use userdata to implement client storage under IE
{
var loaded = ''; //The currently loaded file name

this.udObj = getObject(udObj);
this.udObj.style.behavior = 'url(#default #userdata)';
this.value = this.udObj.value;
this.inhtml = this.udObj.innerHTML;

//Check whether the file exists, est=undefined exists and return true otherwise it returns false
this.exist = function(filename){
try{
me.udObj.load(filename);//Load the XML with the file name filename into
me.load ed = filename; > if( me.loaded=='' || me.loaded!=filename){me.exist(filename);}
                  return me.loaded; this.getAtrib = function(filename,atrib){
                                                                                ​return val==null?"":val;
          }return ""; me.udObj.removeAttribute(atrib);
me.udObj.save(filename); //Save the object data to an XML file named filename
return true; Set the specified attribute value
this.setAtrib = function(filename,atrib,val,expire){
var etime = typeof(expire)=="undefined"?24*60*60:expire;
me.udobj.expires = me.Setexpire (etime);
me.udobj.setattribute (atrib, value;
me.udobj.save (filename); The object data of the series (i.e. the entire XML file) expires
this.remPartion = function(filename){
if(me.exist(filename))
{
me.udObj.expires = me. setExpire(-1);
me.udObj.save(filename);
me.udObj.save(filename);
me.udObj.save(filename)
//Set the validity period
this.setExpire = function(sec){
var oTimeNow = new Date();
oTimeNow.setSeconds(oTimeNow.getSeconds() parseInt(sec));
return oTimeNow.toUTCString();
}
}else // Use globalStorage to implement client storage under non-IE
{
var domain = document.domain;

// Get the specified attribute value
this.getAtrib = function(filename,atrib){
var oTimeNow = new Date();
var etime = parseInt(window.globalStorage[domain][filename "__expire"] ); '';
                                                >               return window.globalStorage[domain][filename "__" atrib];                                                                                                                                                      >            try{window.globalStorage.removeItem(filename "__" atrib);}catch(e){}//Delete
                                                                                                                                                                                 🎜> this.setAtrib = function(filename,atrib,val,expire){
var etime = typeof(expire)=="undefined"?24*60*60:expire;
window.globalStorage[domain] [filename "__expire"] = me.setExpire(etime);
     window.globalStorage[domain][filename "__" atrib] = val; Data invalidation
this.remPartion = function(filename){
me.remAtrib(filename,"expire");
return true;
}

/ /Set validity period
this.setExpire = function(sec){
var oTimeNow = new Date();
oTimeNow.setSeconds(oTimeNow.getSeconds() parseInt(sec));
return oTimeNow.getTime ();
                                                       It has not been used in actual projects, so I don’t know its compatibility and stability. If netizens find bugs, please point them out. Thanks
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!