Use userdata and localstorage for cross-browser Local Storage

Source: Internet
Author: User

From:

Http://www.dewen.org/q/4108/h5%E7%9A%84localStorage%E5%92%8C%E6%9C%AC%E5%9C%B0%E5%AD%98%E5%82%A8IE6%E3%80%81IE7%E4%B8%8D%E6%94%AF%E6%8C%81

1. browser support
Userdata is the storage space opened by Microsoft for IE in the system. Therefore, only Windows + IE is supported. Unexpectedly, userdata has been started since ie5.5.

2. storage location
In XP, it is generally located in c: \ Documents ents and Settings \ USERNAME \ userdata, sometimes in c: \ Documents ents and Settings \ USERNAME \ Application Data \ Microsoft \ Internet Explorer \ userdata.
In Vista, It is located at c: \ Users \ User Name \ appdata \ roaming \ Microsoft \ Internet Explorer \ userdata.
Userdata is saved as an XML file.

3. Size Limit
Security Zone document limit (Kb) domain limit (KB)
Local Machine 128 1024
Intranet 512 10240
Trusted Sites 128 1024
Internet 128 1024
Restricted 64-640
For online use, the size of a single file is limited to kb, and the size of a file under a domain name is limited to kb. There should be no limit on the number of files. In a restricted site, these two values are 64kb and 640kb respectively. Therefore, it is best to control a single file under 64kb if you consider various situations.

4. Use
Userdata can be bound to almost all tags.
Instructions are added to the official documents:
Setting the userdata behavior class on the HTML, Head, title, or style object causes an error when the Save or load method is called.

Apply:
A, abbr, acronym, address, area, B, big, BLOCKQUOTE, button, caption, center, cite, code, DD, Del, dfn, Dir, Div, DL, DT, em, Font, form, HN, HR, I, IMG, input type = button, input type = checkbox, input type = file, input type = hidden, input type = image, input type = password,
Input type = radio, input type = reset, input type = submit, input type = text, KBD, label, Li, listing, MAP, marquee, menu, object, ol, option, p, plaintext, pre, Q, S, SAMP, select, small, span, strike, strong, sub, sup, table, textarea, TT, U, UL, VAR, XMP

You can use style or js to create objects that support userdata.
Html
Create JS

 
 
  1. O = Document. createelement ('input ');
  2. O. type = 'ddn ';
  3. O. addbehavior ('# default # userdata ');
  4. // Equivalent to O. style. Behavior = "URL ('# default # userdata ')";
  5. Document. Body. appendchild (O );

Userdata provides the following attributes and methods:

Attribute
Expires setting or read expiration time
Xmldocument reads the xml dom of an object

Method
Getattribute reads the value of a specified attribute
Load Open File
Removeattribute: deletes a specified attribute.
Save save file
Setattribute: assigns a value to a specified attribute.

5. directory restrictions
Localstorage cannot be accessed across regions, while userdata is more strict and cannot be accessed across directories.
For example:

Http://example.com/path1

You can only access the webpage file at http://example.com/path1/example.php.
All files in the http://example.com/path1/path2directory are not stored in path1.

Cookie can be accessed across subdomains by setting domain.

In this case, why not use cookies for local storage?
1. Small capacity, about 4 kb
2. Cookie may be disabled
3. Insufficient cookie Security
4. Each cookie is sent to the server to increase the bandwidth. This is not consistent with our local storage purpose.
4. javascript disabled

6. Encapsulation

 
 
  1. typeof window.localStorage == 'undefined' && ~function(){
  2.  
  3. var localStorage = window.localStorage = {},
  4.     prefix = 'data-userdata',
  5.     doc = document,
  6.     attrSrc = doc.body,
  7.     html = doc.documentElement,
  8.  
  9.     // save attributeNames to
  10.     // data-userdata attribute
  11.     mark = function(key, isRemove, temp, reg){
  12.  
  13.         html.load(prefix);
  14.         temp = html.getAttribute(prefix);
  15.         reg = RegExp('\\b' + key + '\\b,?', 'i');
  16.  
  17.         hasKey = reg.test(temp) ? 1 : 0;
  18.  
  19.         temp = isRemove ? temp.replace(reg, '').replace(',', '') :
  20.                 hasKey ? temp : temp === '' ? key :
  21.                     temp.split(',').concat(key).join(',');
  22.  
  23.  
  24.         html.setAttribute(prefix, temp);
  25.         html.save(prefix);
  26.  
  27.     };
  28.  
  29. // add IE behavior support
  30. attrSrc.addBehavior('#default#userData');
  31. html.addBehavior('#default#userData');
  32.  
  33. //
  34. localStorage.getItem = function(key){
  35.     attrSrc.load(key);
  36.     return attrSrc.getAttribute(key);
  37. };
  38.  
  39. localStorage.setItem = function(key, value){
  40.     attrSrc.setAttribute(key, value);
  41.     attrSrc.save(key);
  42.     mark(key);
  43. };
  44.  
  45. localStorage.removeItem = function(key){
  46.     attrSrc.removeAttribute(key);
  47.     attrSrc.save(key);
  48.     mark(key, 1);
  49. };
  50.  
  51. // clear all attributes on <body> that using for textStorage
  52. // and clearing them from the 'data-userdata' attribute's value of
  53. localStorage.clear = function(){
  54.  
  55.     html.load(prefix);
  56.  
  57.     var attrs = html.getAttribute(prefix).split(','),
  58.         len = attrs.length;
  59.  
  60.     for(var i=0;i<len;i++){
  61.         attrSrc.removeAttribute(attrs[i]);
  62.         attrSrc.save(attrs[i]);
  63.     };
  64.  
  65.     html.setAttribute(prefix,'');
  66.     html.save(prefix);
  67.  
  68. };
  69.  
  70. }();

7. available frameworks
(1) store. js
Store. JS is a lightweight JS framework.
Store. Set ('key', 'value') and store. Get ('key', 'value') basically meet the requirements. If it is stored and parsed in JSON format, JSON. js should be used to make ie support JSON objects. In addition to the native JavaScript, you can also find the jquery version of store. js.
(2) Others
Ustore. js https://github.com/hugeinc/USTORE.js.
Box. js https://github.com/kbjr/Box.js

8. Reference

Http://news.ycombinator.com/item? Id = 1468802

Http://msdn.microsoft.com/en-us/library/ms531424%28v=VS.85%29.aspx

Http://www.cnblogs.com/QLeelulu/archive/2008/03/29/1129322.html

Http://sofish.de/1872

As for localstorage, I will not talk about it.

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.