使用 Javascript 确定文件是否存在(本地)
上次得到了一些很好的反馈。
我本质上是在我的本地计算机上设置一个 Wiki 类型的系统。我在 HTML 文件之间进行链接,其中一些已经存在,一些还没有。我正在使用 XMLHttpRequest 检查文件是否存在。
当我需要检查不同目录中的文件时,问题就出现了。我发现这是一个安全措施。但是,如果可能的话,我想仅针对我的本地文件禁用它。这似乎很奇怪,我根本无法使用简单的 Javascript 来返回任何网页上的文本,但在我自己的计算机上更是如此。
我没有运行任何类型的服务器软件,只是一堆包含 Javascript 的 HTML 文件。
提前致谢!
got some great feedback last time.
I'm essentially setting up a Wiki-type system on my local computer. I'm linking between HTML files, with some already existing and some not. I'm using XMLHttpRequest to check to see if the files exist.
The problem comes when I need to check files that are in different directories. I've come to find out this is a security thing in place. However, I'd like to disable it if possible, just for my local files. It seems rather odd that I can't have simple Javascript that returns the text on any web page at all, but even more so on my own computer.
I don't have any sort of server software running, just a bundle of HTML files with Javascript in them.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
javascript 无法与本地文件系统通信。这样做的原因是为了防止人们编写破坏性脚本来破坏您的计算机。
javascript can not talk to the local filesystem. The reason for this is to keep people from writing destructive scripts that break your computer.
如果这不是学习练习,也许您可能对 TiddlyWiki 感兴趣?
If this isn't a learning exercise, perhaps you might be interested in TiddlyWiki?
正如您提到自己时,“这是一个适当的安全措施”。一般来说,JavaScript 不允许访问本地文件,因为它违反了沙箱。
但是,请查看这些讨论一些可能的解决方案的 SO 帖子:
您还可以看看这个HTML5 功能:
As you mention your self, "this is a security thing in place". In general, JavaScript is not allowed to access local files as it violates the sandbox.
However, have a look at these SO posts that discuss a few possible solutions:
You can also have a look at this HTML5 feature: