使用JavaScript写入文本文件? [英] Use JavaScript to write to text file?

查看:129
本文介绍了使用JavaScript写入文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从我的一个博客页面中捕获搜索的主题,并将其写入文本文件以进行跟踪。我已经能够捕获输入字符串,但是无法将其写入文本文件。我已阅读并尝试实施我发现的所有解决方案,但均未成功。我提供了一个非常简单的代码示例,可将文本写入文件,但无法正常工作。 Onblur事件函数可以很好地访问myFunction,因此没有问题。

I have a need to capture the topics of a search from one of my blog pages and write it to text file for followup. I have been able to capture the input string, however, am not able to write it to a text file. I have read and tried to implement all the solutions I have found with no success. I include a very simple sample of code to write text to file but I can't get it to work. The Onblur event functions fine to access myFunction, so that's not problem. I'm going crazy as samples shown from others are so simple.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" >
    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>fs=Server CreateObject ("Scripting FileSystemObject")</title>
    <script type="text/javascript">
    function myFunction()
    {
        var fs = ActiveXObject("Scripting.FileSystemObject");
        var f = fs.OpenTextFile("d:\\test\\topics.txt", 8, true);
        f.WriteLine("This text will be added to the end of file");
        f.Close();
    }
    </script>
    </head>

    <body>
    <input name="Text1" type="text" size="55" onblur="myFunction()" />

    </body>

    </html>


推荐答案

JavaScript 不能从客户端读取/写入文件或直接向服务器写入文件。

JavaScript cannot read/write files from the client or write files to the server (directly).

如果要将文件写入服务器,请考虑使用将与javascript交互的服务器端脚本-和示例,了解如何使用少量PHP来做到这一点。

If you want to write files to the server consider using server-side scripts that will interact with your javascript - and example on how to do this with a little bit of PHP.

就客户端文件交互而言,出于安全原因,这是根本不可能的(除非将其上载到脚本中,以供读取)。

In terms of client file interaction this is simply impossible for security reasons (unless it is uploaded to the script in which case it is read accessible).

但是您可以在客户端上存储数据-我以前一直在使用 jStorage 并强烈推荐它。它可以跨浏览器运行(即使在IE 6上也是如此),并且非常易于使用。

You can however store data on clients - I have previously been using jStorage and highly recommend it. It works cross-browser (even on IE 6) and is extremely easy to use.

这篇关于使用JavaScript写入文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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