最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

activex - Use JavaScript to write to text file? - Stack Overflow

programmeradmin5浏览0评论

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="">

    <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>

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/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>
Share Improve this question edited Nov 18, 2012 at 0:53 Brad 164k56 gold badges377 silver badges553 bronze badges asked Nov 18, 2012 at 0:49 RobertRobert 291 gold badge1 silver badge2 bronze badges 4
  • 1 Do you want to write the file to the client or server? You won't have any luck writing to the server with JavaScript. – jahroy Commented Nov 18, 2012 at 0:53
  • Where did you get the sample code, and what browser are you trying to get this to work on? ActiveX objects only work with Internet Explorer... and I don't know if they work with newer versions of Internet Explorer. – Richard JP Le Guen Commented Nov 18, 2012 at 0:53
  • 4 Also, ActiveX only works for IE. I'd remend forgetting about it. – jahroy Commented Nov 18, 2012 at 0:54
  • HTML5 can only let you write text files to the client not server. – Derek 朕會功夫 Commented Nov 18, 2012 at 1:56
Add a ment  | 

4 Answers 4

Reset to default 1

If your're using modern browsers which support data-uri, you can create content with javascript and force a download by base64 encoding it and making it patible with data-uri, then simply pointing window location at it.

This answer demonstrates an HTML-only way to do it.

You can also use a flash helper to take care of this. For instance, Downloadify does this.

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

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).

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

Thanks for all the suggestions provided by the members, I was able to perform writing and appending to text file on server. I used html code to acquire my data needed for writing and then called a .php file to do the writing to file. It was a client side versus server side issue and the .php solved it, using fopen, fwrite and fclose.

Techically, when using javascript in a (.hta) file, you can use the ActiveXObject Scripting.FileSystemObject to access the filesystem, with it there are all the mands you need to do anything to the filesystem that you have permission to do.

var fso=new ActiveXObject("Scripting.FileSystemObject");
if(fso.FolderExists("C:\\Users\\")){
  alert("It exists, hello friend!");
}eles{
  alert("Doesn't exist, who are you?");
}
发布评论

评论列表(0)

  1. 暂无评论