使用 Javascript 在本地保存 HTML [英] Save HTML locally with Javascript

查看:22
本文介绍了使用 Javascript 在本地保存 HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道出于安全原因,Javascript 无法在文件系统中写入数据.我经常读到,使用 Javascript 在本地保存数据的唯一方法是 cookie 或 localStorage.

但是是否有可能,在非常特殊的情况下,本地(而不是通过互联网)访问文件以本地写入数据?(没有任何服务器语言,甚至根本没有任何服务器......只是本地浏览一个 HTML 文件)

例如,是否可以通过点击此处的保存:

<块引用>

用户代理应提供暴露给脚本的 API,该 API 暴露上述功能.用户在与文件系统交互的任何时候都会收到 UI 通知,让用户可以完全取消或中止事务.用户会收到任何文件选择的通知,并且可以取消这些选择.在没有用户干预的情况下,不会以静默方式调用这些 API.

基本上,由于安全设置,每次下载文件时,浏览器都会确保用户确实想要保存文件.浏览器并没有真正区分计算机上的 JavaScript 和 Web 服务器上的 JavaScript.唯一的区别是浏览器访问文件的方式,因此将页面存储在本地不会有任何区别.

解决方法:但是,您可以将 <div> 的 innerHTML 存储在 cookie 中.当用户回来时,您可以从 cookie 中加载它.虽然它并没有完全将文件保存到用户的计算机上,但它应该具有与覆盖文件相同的效果.当用户返回时,他们将看到上次输入的内容.缺点是,如果用户清除他们的网站数据,他们的信息就会丢失.由于忽略用户清除本地存储的请求也是一个安全问题,因此确实没有办法绕过它.

但是,您也可以执行以下操作:

  • 使用 Java 小程序
  • 使用其他类型的小程序
  • 创建桌面(非基于 Web)应用
  • 请记住在清除网站数据时保存文件.您可以创建一个警报,在您退出页面时弹出并提醒您,甚至为您打开保存窗口.

使用 cookie:可以在本地页面上使用 JavaScript cookie.只需将其放入文件中并在浏览器中打开即可:

<头><身体><p id="timesVisited"></p><script type="text/javascript">var timesVisited = parseInt(document.cookie.split("=")[1]);如果 (isNaN(timesVisited)) timesVisited = 0;次访问++;document.cookie = "timesVisited=" + timesVisited;document.getElementById("timesVisited").innerHTML = "你运行了这个片段" + timesVisited + " times.";

I do know that Javascript cannot write data in the filesystem, for security reasons. I have often read that the only way to save data locally with Javascript is cookies or localStorage.

But is it possible, in the very specific case when the file is accessed locally (and not through internet) to write data locally ? (without any server language, and even without any server at all... just local browsing of a HTML file)

For example, would it be possible, by clicking on SAVE here on this page ...

...that the HTML file is overwritten with its new content ? (i.e. the local HTML file should be updated when SAVE is pressed).



Is this possible to SAVE a file thanks to Javascript, when the HTML page is accessed locally?

Note: I want to be able to silently save, not propose a Download/Save dialog box in which the user has to choose where to download, then "Are you sure to want to overwrite" etc.


EDIT : Why this question? I would like to be able to do a HTML/JS notepad that I can run locally without any server (no apache, no php). I need to be able to save easily without having to deal with Dialog Box "Where do you want to download the file?".

解决方案

The canonical answer, from 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.

Basically, because of security settings, any time you download a file, the browser will make sure the user actually wants to save the file. Browsers don't really differentiate JavaScript on your computer and JavaScript from a web server. The only difference is how the browser accesses the file, so storing the page locally will not make a difference.

Workarounds: However, you could just store the innerHTML of the <div> in a cookie. When the user gets back, you can load it back from the cookie. Although it isn't exactly saving the file to the user's computer, it should have the same effect as overwriting the file. When the user gets back, they will see what they entered the last time. The disadvantage is that, if the user clears their website data, their information will be lost. Since ignoring a user's request to clear local storage is also a security problem, there really is no way around it.

However, you could also do the following:

  • Use a Java applet
  • Use some other kind of applet
  • Create a desktop (non-Web based) application
  • Just remember to save the file when you clear your website data. You can create an alert that pops up and reminds you, or even opens the save window for you, when you exit the page.

Using cookies: You can use JavaScript cookies on a local page. Just put this in a file and open it in your browser:

<!DOCTYPE html>
<html>

<head>
</head>

<body>
  <p id="timesVisited"></p>
  <script type="text/javascript">
    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.";
  </script>
</body>

</html>

这篇关于使用 Javascript 在本地保存 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆