CFXIXI工作室首页
CF西西的博客 | ie存储数据(使用userdata)

ie存储数据(使用userdata)

4. 六月 2013

ie存储数据(使用userdata)

(new function () {

    if (window.localStorage) return;

    this.userData = null;
    this.name = location.hostname;
    //this.name = "wangye.org";

    if (!this.userData) {
        try {
            this.userData = document.createElement('INPUT');
            this.userData.type = "hidden";
            this.userData.style.display = "none";
            this.userData.addBehavior("#default#userData");
            document.body.appendChild(this.userData);
            var expires = new Date();
            expires.setDate(expires.getDate() + 365);
            this.userData.expires = expires.toUTCString();
            window.userData  = this;
        } catch (e) {
        }
    }

    this.setItem = function (key, value) {
        this.userData.load(this.name);
        this.userData.setAttribute(key, value);
        this.userData.save(this.name);
    };

    this.getItem = function (key) {
        this.userData.load(this.name);
        return this.userData.getAttribute(key);
    };

    this.removeItem = function (key) {
        this.userData.load(this.name);
        this.userData.removeAttribute(key);
        this.userData.save(this.name);
    };
}());

javascript