zoukankan      html  css  js  c++  java
  • userData

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    
    </head>
    <body>
    <input type="text" name="editor" id="editor"/><br/>
    <input type="button" name="delete" id="delete" value='delete'/>
    <br/>
    <input type="button" name="save" id="save" value='save'/><br/>
    <input type="button" name="reload" id="reload" value='reload'/>
    <script type="text/javascript">
    var editor = document.getElementById("editor");
    editor.value="";
    var UserData = {
        userData : null,
        name : location.hostname,
       
    
        init:function(){
            if (!UserData.userData) {
                try {
                    UserData.userData = document.createElement('INPUT');
                    UserData.userData.type = "hidden";
                    UserData.userData.style.display = "none";
                    UserData.userData.addBehavior ("#default#userData");
                    document.body.appendChild(UserData.userData);
                    var expires = new Date();
                    expires.setDate(expires.getDate()+365);
                    UserData.userData.expires = expires.toUTCString();
                } catch(e) {
                    return false;
                }
            }
            return true;
        },
    
        setItem : function(key, value) {
    
            if(UserData.init()){
                UserData.userData.load(UserData.name);
                UserData.userData.setAttribute(key, value);
                UserData.userData.save(UserData.name);
            }
        },
    
        getItem : function(key) {
            if(UserData.init()){
            UserData.userData.load(UserData.name);
            return UserData.userData.getAttribute(key)
            }
        },
    
        remove : function(key) {
            if(UserData.init()){
            UserData.userData.load(UserData.name);
            UserData.userData.removeAttribute(key);
            UserData.userData.save(UserData.name);
            }
    
        }
    };
    
    window.onbeforeunload=function(){
        console.log(document.getElementById("editor").value);
        if(!window.localStorage){
            UserData.setItem('editor-text',editor.value);
        }else{
            localStorage.setItem('editor-text',editor.value);
        }
    
    };
    window.onload=function(){
        console.log(document.getElementById("editor").value);
        if(!window.localStorage){
            if(UserData.getItem('editor-text')==undefined) {
                editor.value="";
            }else{
                editor.value=UserData.getItem('editor-text');
            }
        }else{
            if(localStorage.getItem('editor-text')!=null){
                editor.value=localStorage.getItem('editor-text');
            }else{
                editor.value="";
            }
        }
    
    };
    document.getElementById("delete").onclick=function(){
        if(!window.localStorage){
            UserData.remove('editor-text');
        }else{
            localStorage.removeItem('editor-text');
        }
    }
    document.getElementById("reload").onclick=function(){
        if(!window.localStorage){
            document.getElementById("editor").value=UserData.getItem('editor-text');
        }else{
            document.getElementById("editor").value=localStorage.getItem("editor-text");
        }
    }
    document.getElementById("save").onclick=function(){
        if(!window.localStorage){
            
            UserData.setItem('editor-text',document.getElementById("editor").value);
            console.log(UserData.getItem('editor-text'));
        }else{
            console.log(document.getElementById("editor").value);
            localStorage.setItem('editor-text',document.getElementById("editor").value);
            console.log(localStorage.getItem("editor-text"));
        }
    }
    
    </script>
    </body>
    </html>
  • 相关阅读:
    STM32 硬件I2C 到底是不是个坑?
    memory cache 和 disk cache
    希尔排序为什么不稳定
    17-18专业课
    fread和fseek的用法
    浅析alsa声卡驱动snd_interval结构体openmin,openmax和integer含义
    动态存储区、静态存储区、堆和栈的区别
    【专家坐堂Q&A】在 petalinux-config 中选择外部来源时,可将符号链路添加内核来源目录树
    模型文件后缀介绍
    @RestController注解
  • 原文地址:https://www.cnblogs.com/Lennyyi/p/userData.html
Copyright © 2011-2022 走看看