规范的答案,从
the W3C File API Standard:
User agents should provide an API exposed to script that exposes the features above. The user is notified by UI anytime interaction with the file system takes place, giving the user full ability to cancel or abort the transaction. The user is notified of any file selections, and can cancel these. No invocations to these APIs occur silently without user intervention.
基本上,由于安全设置,任何时候您下载文件,浏览器将确保用户实际上要保存文件.浏览器并没有真正区别您的计算机上的JavaScript和JavaScript与Web服务器.唯一的区别是浏览器如何访问文件,因此在本地存储页面将不会有所作为.
解决方法:
但是,您可以只存储< div>在一个cookie.当用户回来时,您可以从cookie加载它.虽然它不是完全将文件保存到用户的计算机,但它应该具有与覆盖文件相同的效果.当用户回来时,他们将看到他们上次进入的内容.缺点是,如果用户清除他们的网站数据,他们的信息将会丢失.由于忽略用户清除本地存储的请求也是一个安全问题,所以没有办法.
但是,您也可以执行以下操作:
>使用Java小程序
>使用其他类型的applet
>创建一个桌面(非基于Web)的应用程序
>只要记住在清除网站数据时保存文件.当您退出页面时,您可以创建弹出并提醒您的警报,甚至为您打开保存窗口.
使用Cookie:您可以在本地页面上使用JavaScript Cookie.只需将其放在一个文件中,并在浏览器中打开它:
var timesVisited = parseInt(document.cookie.split("=")[1]);
if (isNaN(timesVisited)) timesVisited = 0;
timesVisited++;
document.cookie = "timesVisited=" + timesVisited;
document.getElementById("timesVisited").innerHTML = "You ran this snippet " + timesVisited + " times.";