Cross-browser Local Storage Solution

Source: Internet
Author: User

Cross-browser local storage:
1. localstorage: Only Internet Explorer 8 +, Firefox, chrome, and opera are supported. Browsers with less than Internet Explorer 8 are not supported.
2. browser cookies: a relatively small amount of data storage is supported. Each domain can have a maximum of 20 cookies. Each cookie cannot exceed 4 kb in length. Otherwise, it will be truncated, and some Browsers Do not even support it; at the same time, the cookie has security issues. If the cookie is intercepted, all session information can be obtained.
3. You can embed a hidden flash on the page and then use the flash plug-in dobject of flash. It basically does not have compatibility issues. Only Flash and JS need to be introduced separately, however, this will increase the page load.
4. User Data: it is a storage space specially opened by Microsoft for ie in the system (this supports all IE browsers ), only Windows + IE combinations are supported. (the size of a single file is limited to kb. a domain name can store a total of KB of files. There should be no limit on the number of files. In a restricted site, these two values are 64kb and 640kb respectively .)
To sum up, we can use localstorage + User data as a solution for local storage:

(function (window) {    window.localDataStore = {        hname: location.hostname ? location.hostname : 'localStatus',        isLocalStorage: window.localStorage ? true : false,        dataDom: null,        initDom: function () {            if (!this.dataDom) {                try {                    this.dataDom = document.createElement('input');                     this.dataDom.type = 'hidden';                    this.dataDom.style.display = "none";                    this.dataDom.addBehavior('#default#userData');                     document.body.appendChild(this.dataDom);                    var exDate = new Date();                    exDate = exDate.getDate() + 30;                    this.dataDom.expires = exDate.toUTCString();                 } catch (ex) {                    return false;                }            }            return true;        },        set: function (key, value) {            if (this.isLocalStorage) {                window.localStorage.setItem(key, value);            } else {                if (this.initDom()) {                    this.dataDom.load(this.hname);                    this.dataDom.setAttribute(key, value);                    this.dataDom.save(this.hname)                }            }        },        get: function (key) {            if (this.isLocalStorage) {                return window.localStorage.getItem(key);            } else {                if (this.initDom()) {                    this.dataDom.load(this.hname);                    return this.dataDom.getAttribute(key);                }            }        },        remove: function (key) {            if (this.isLocalStorage) {                localStorage.removeItem(key);            } else {                if (this.initDom()) {                    this.dataDom.load(this.hname);                    this.dataDom.removeAttribute(key);                    this.dataDom.save(this.hname)                }            }        }    }})(window);

 

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.