介绍:
应用程序必须能够完全离线运行,本地存储数据,并在有网络连接时通过AJAX在线发布数据 - 这可能会在几天后进行。
问题:
如何使用Javascript存储数据?
附加说明:
- 我不想使用任何服务器端技术。
- 它必须像数据库一样安全。我已经阅读了关于cookie和html5存储的内容,但它们都不令人信服。
应用程序必须能够完全离线运行,本地存储数据,并在有网络连接时通过AJAX在线发布数据 - 这可能会在几天后进行。
如何使用Javascript存储数据?
// Save data to a the current session's store
sessionStorage.setItem("username", "John");
// Access some stored data
alert( "username = " + sessionStorage.getItem("username"));
// Get the text field that we're going to track
var field = document.getElementById("field");
// See if we have an autosave value
// (this will only happen if the page is accidentally refreshed)
if ( sessionStorage.getItem("autosave")) {
// Restore the contents of the text field
field.value = sessionStorage.getItem("autosave");
}
// Check the contents of the text field every second
setInterval(function(){
// And save the results into the session storage object
sessionStorage.setItem("autosave", field.value);
}, 1000);
您可以使用HTML5本地存储
对于旧浏览器,请使用polyfill https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills#web-storage-localstorage-and-sessionstorage
你想要的正是这个: