Javascript globalStorage code

Source: Internet
Author: User

GlobalStorage
This is also proposed in html5. After the browser is closed, the information stored in globalStorage can still be retained, and the storage capacity is much larger than that of userdata in IE. The storage capacity of a domain is 5120 kb. 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 can use this space.
GlobalStorage ['com']: This space can be shared by all com domain names.
GlobalStorage ['']: space available for all pages
Currently, Firefox only supports globalStorage in the current domain. If a public domain is used, a similar error "Security error" code: "1000" may occur ".
Expiration time
According to the HTML5 description, globalStorage will expire only when there is a security issue or when the user requires it. the browser should avoid deleting the data that is being accessed by the script, and userdata should be writable by the user.
Therefore, our scripts must be able to control the expiration time. They can store the expiration time in a certain region of globalStorage and determine whether the expiration time is expired during load. This can solve the expiration time problem to some extent.
Storage expiration time
The above is the information I found from the Internet. In order to be compatible with the non-IE browser "userdata", I improved
"Userdata" (see UserData usage summary) is now compatible with IE and supports globalStorage browsers.
Copy codeThe Code is as follows:
Function behaviorUserdata (udObj)
{
Var me = this;
If (CMInfo. Bs_Name = 'ie') // use userdata in IE to implement client Storage
{
Var loaded = ''; // name of the currently loaded file

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. If est = undefined exists, true is returned. Otherwise, false is returned.
This. exist = function (filename ){
Try {
Me. udObj. load (filename); // load the XML file named filename
Me. loaded = filename;
Return true;
} Catch (e) {return false ;}
}
// Pre-load
This. preLoad = function (filename ){
If (me. loaded = ''| me. loaded! = Filename) {me. exist (filename );}
Return me. loaded;
}
// Obtain 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 the object
This. remAtrib = function (filename, atrib ){
Me. udObj. removeAttribute (atrib );
Me. udObj. save (filename); // save the object data to an XML file named filename.
Return true;
}
// Set 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 a series of object data (that is, the entire XML file) to be invalid
This. remPartion = function (filename ){
If (me. exist (filename ))
{
Me. udObj. expires = me. setExpire (-1 );
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 for client storage in non-IE Environments
{
Var domain = document. domain;

// Obtain 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 the object
This. remAtrib = function (filename, atrib ){
Try {window. globalStorage. removeItem (filename + "_" + atrib);} catch (e) {}// Delete
Return true;
}

// Set 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 a series of object data to fail
This. remPartion = function (filename ){
Me. remAtrib (filename, "expire ");
Return true;
}

// Set the validity period
This. setExpire = function (sec ){
Var oTimeNow = new Date ();
OTimeNow. setSeconds (oTimeNow. getSeconds () + parseInt (sec ));
Return oTimeNow. getTime ();
}
}
}

For the CMInfo class, see some common JS functions (1) (updated on)
It should be noted that it has not been used in the actual project, so I still don't know how compatible and stable it is. If a user finds a BUG, I hope to point it 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.