完成 - 编辑一次
我希望创建一个具有持久性内存的 Like Counter !
现在,我的项目存储在USB驱动器上,我还没有考虑将我的半成品网站上传到互联网。我带着它,堵塞和工作
该网站的一个功能是心脏计数器和 Like Counter ,分别带有符号图标。
我有一个小副业JavaScript文件,它有十几个函数来处理点击事件等 - 例如计数器的数量计数。
但是,由于计数器的值自动分配到临时存储器 - 如果您要重新加载页面 - 计数器号将重置为它&#39 ; s默认值,零。一个巨大的头痛......
我想过使用实验ReadFile()
对象来处理问题 - 但我很快发现它需要一个用户放置文件来操作(从我的考试中)。
这是我的尝试:
if (heartCount || likeCount >= 1) {
var reader = new FileReader();
var readerResults = reader.readAsText(heartsAndLikes.txt);
//return readerResults
alert(readerResults);
}
加载后,页面将按标准操作运行,但上述情况除外 在我看来,这将是理想的解决方案......
现在,Cookie似乎不是一个选项,因为它位于每台计算机的基础上 它们存储在计算机的SSD中,而不是存储在JavaScript文件中... ... ...
使用新的Web存储可能会有很大的帮助。但同样,无论系统多么漂亮,它都是基于每台计算机的......
localStorage.heartCount = 0 //Originally...
function heartButtonClicked() {
if (localStorage.heartCount) {
localStorage.heartCount = Number(localStorage.heartCount) + 1
}
document.getElementById('heartCountDisplay').innerHTML = localStorage.heartCount
} //Function is tied to the heartCountButton directly via the 'onclick' method
但是,我怀疑是否可以在USB驱动器上继承网页存储......
目前,我正在寻找阅读和编辑文件,因为它对我的情况最为理想。但是......
你会用哪个?你会介绍一种新方法吗?
拜托,告诉我吧! :)
答案 0 :(得分:0)
if (typeof(Storage) !== "undefined") { //make sure local storage is available
if (!localStorage.heartCount) { //if heartCount is not set then set it to zero
localStorage.heartCount = 0;
}
} else {
alert('Local storage is not available');
}
function heartButtonClicked() {
if (localStorage.heartCount) { //if heartCount exists then increment it by one
localStorage.heartCount++;
}
//display the result
document.getElementById('heartCountDisplay').innerHTML = localStorage.heartCount
}
这仅适用于每台计算机,并且不会在您的拇指驱动器上保留。我能想到的唯一方法是在驱动器上保留数据是手动下载JSON或文本文件。