JavaScript globalstorage class Code _javascript Tips

Source: Internet
Author: User
Globalstorage
This is also proposed in the HTML5, after the browser is closed, the use of globalstorage stored information can still be retained, and storage capacity than IE userdata much larger, a domain below 5120k. As with Sessionstorage, the information stored in any page of a domain can be shared by all pages.
Scope
globalstorage[' z.baidu.com '] all z.baidu.com The following pages can use this space
globalstorage[' baidu.com '] all baidu.com The following pages can use this space
globalstorage[' com ': All COM domain names can be shared using this space
Globalstorage[']: space available for all pages
Now Firefox only supports Globalstorage storage in the current domain, if using a common domain can cause a similar error like "Security Error" code: "1000".
Expiration time
According to HTML5, Globalstorage only expires on security issues or when users require it, browsers should avoid deleting data that is being accessed by scripts, and UserData should be user-writable.
So our script will be able to control the expiration time, can store the expiration time in the Globalstorage area, when the load time determines whether expires, may solve the expiration time problem to some extent.
When stored, the expiration time is stored at the same time
The above is the information I have queried from the Internet, in order to be compatible with non IE browser "UserData", I improved my own writing before a
"UserData" (see UserData Usage Summary), now is compatible with IE and support Globalstorage browser.
Copy Code code as follows:

function Behavioruserdata (udobj)
{
var me = this;
if (cminfo.bs_name== ' IE ')//ie use UserData to implement client-side storage
{
var loaded = '; The file name that is currently loaded

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, exists est=undefined, returns True, or returns false
this.exist = function (filename) {
try{
Me.udObj.load (filename)//XML loaded with file name filename
me.loaded = filename;
return true;
}catch (e) {return false;}
}
Pre-loading
This.preload = function (filename) {
if (me.loaded== ' | | me.loaded!=filename) {me.exist (filename);}
return me.loaded;
}
Gets the specified property value
This.getatrib = function (filename,atrib) {
if (me.preload (filename)!= ')
{
var val = me.udObj.getAttribute (Atrib);
Return val==null? ": Val;
}return "";
}
Remove the specified property of an object
This.rematrib = function (filename,atrib) {
Me.udObj.removeAttribute (Atrib);
Me.udObj.save (filename); Save object data to an XML file named filename
return true;
}
Sets the specified property 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,val);
Me.udObj.save (filename);
}
Set the object data for a series (that is, the entire XML file) invalidated
this.rempartion = function (filename) {
if (me.exist (filename))
{
Me.udObj.expires = Me.setexpire (-1);
Me.udObj.save (filename);
}
}
Set Expiration
This.setexpire = function (sec) {
var otimenow = new Date ();
Otimenow.setseconds (Otimenow.getseconds () + parseint (sec));
return otimenow.toutcstring ();
}
}else//Non IE use Globalstorage to implement client storage
{
var domain = Document.domain;

Gets the specified property value
This.getatrib = function (filename,atrib) {
var otimenow = new Date ();
var etime = parseint (window.globalstorage[domain][filename + "__expire"));
if (!etime | | etime < parseint (Otimenow.gettime ()))
{
Me.rempartion (filename);
Return ";
}
return window.globalstorage[domain][filename + "__" + Atrib];
}

Remove the specified property of an object
This.rematrib = function (filename,atrib) {
Try{window.globalstorage.removeitem (filename + "__" + Atrib);} catch (e) {}//Delete
return true;
}

Sets the specified property value
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;
}

Set object data for a series to fail
this.rempartion = function (filename) {
Me.rematrib (filename, "expire");
return true;
}

Set Expiration
This.setexpire = function (sec) {
var otimenow = new Date ();
Otimenow.setseconds (Otimenow.getseconds () + parseint (sec));
return Otimenow.gettime ();
}
}
}

Among them Cminfo class see some commonly used JS function function (i) (2009-06-04 update)
It needs to be explained that because it is not yet in the actual project, so I do not know its compatibility and stability, if the user found a bug, also hope to point out. Thank you
Related Article

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.