<%@ 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>
时间: 2024-10-31 08:14:29