// 基于cookie的存储API functionCookieStorage(maxage, path) { // 获取一个存储全部cookie的对象 var cookies = (function() { var cookies = {}; var all = document.cookie; if (all === "") { return cookies; } // 分离出名/值对 var list = all.split("; "); // 遍历每个cookie for (var i = 0; i < list.length; i++) { var cookie = list[i]; var p = cookie.indexOf("="); // 获取cookie的名字 var name = cookie.substring(0, p); // 获取cookie对应的值 var value = cookie.substring(p + 1); // 解码 value = decodeURIComponent(value); cookies[name] = value; } return cookies; }());
// 将所有的cookie存储到一个数组中 var keys = []; for (var key in cookies) { keys.push(key); }
if (idletimer) { clearTimeout(idletimer); } idletimer = setTimeout(save, 5000);
savebutton.disabled = false; }, false); sync(); }; window.onbeforeunload = function() { if (localStorage.lastModified > localStorage.lastSaved) { save(); } }; window.onoffline = function() { status("Offline"); } window.ononline = function() { sync(); }; window.applicationCache.onupdateready = function() { status("A new version of this application is available. Reload to run it"); }; window.applicationCache.onnoupdate = function() { status("You are running the latest version of the application."); }; functionstatus(msg) { statusline.innerHTML = msg; } functionsave() { if (idletimer) { clearTimeout(idletimer); } idletimer = null; if (navigator.onLine) { var xhr = new XMLHttpRequest(); xhr.open("PUT", "/note"); xhr.send(editor.value); xhr.onload = function() { localStorage.lastSaved = Date.now(); savebutton.disabled = true; }; } } functionsync() { if (navigator.onLine) { var xhr = new XMLHttpRequest(); xhr.open("GET", "/note"); xhr.send(); xhr.onload = function() { var remoteModTime = 0; if (xhr.status == 200) { var remoteModTime = xhr.getResponseHeader("Last-Modified"); remoteModTime = newDate(remoteModTime).getTime(); } if (remoteModTime > localStorage.lastModified) { status("Newer note found on server."); var useit = confirm("There is a newer version of the note\n" + "on the server. Click Ok to use that version\n" + "or click Cancel to continue editing this\n" + "version and overwrite the server"); var now = Date.now(); if (useit) { editor.value = localStorage.note = xhr.responseText; localStorage.lastSaved = now; status("Newest version downloaded."); } else { status("Ignoring newer version of the note."); } localStorage.lastModified = now; } else { status("You are editing the current version of the note."); } if (localStorage.lastModified > localStorage.lastSaved) { save(); } editor.disabled = false; editor.focus(); } } else { status("Can't sync while offline"); editor.disabled = false; editor.focus(); } }