IE实现userData 永久存储

 

注意:只支持IE5,及其以上的浏览器

//需要使用 if 条件注释
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>IE 中使用userData来进行持久化数据</title>
    <link rel="stylesheet" href="">
</head>
<body>
    <script>
        window.onload=function(){
            var memory=document.createElement('div');
            memory.id = '_memory';
            memory.style.display = 'none';
            memory.style.behavior = "url('#default#userData')";
            document.body.appendChild(memory);
            memory.load('myStoreData');
            var name = memory.getAttribute('username');
            if(!name){
                var name = prompt('what is your name?');
                memory.setAttribute('username',name);
                memory.save('myStoreData');

            }
        };
        function UserDataStorage(maxage){
            var memory=document.createElement('div');
            memory.id = '_memory';
            memory.style.display = 'none';
            memory.style.behavior = "url('#default#userData')";
            document.body.appendChild(memory);
            if(maxage){
                var now = new Date().getTime();
                var expires = now+maxage*1000;
                memory.expires = new Date(expires).toUTCString();
            }
            memory.load('UserDataStorage');
            this.getItem=function(key){
                return memory.getAttribute(key) || null;
            };
            this.setItem=function(key,value){
                memory.setAttribute(key,value);
                memory.save('UserDataStorage');
            };
            this.removeItem=function(key){
                 memory.removeAttribute(key);
                 memory.save('UserDataStorage');
            };
        }
    </script>
</body>
</html>
原文地址:https://www.cnblogs.com/jijm123/p/8433974.html