简体   繁体   English

如何使用Javascript编写本地.txt

[英]How to write a local .txt with Javascript

I want to write a .txt file using the javascript console in IE. 我想使用IE中的javascript控制台编写.txt文件。 I already have the strings that I would like to write on the file; 我已经有想要写在文件上的字符串了; so, what I would like to do is: 因此,我想做的是:

var refTab=document.getElementById("historymatch_tb0");
var  ttl;
for ( var i = 0; row = refTab.rows[i]; i++) 
{
   row = refTab.rows[i];
   for ( var j = 0; col = row.cells[j]; j++ )
   {
       WriteInFile (col.innerHTML);
   }

}

What I don't have is the function WriteInFile because I triyed this: 我没有的是功能WriteInFile,因为我尝试了以下方法:

 function WriteInFile (a)
 {
  var fso = new ActiveXObject("Scripting.FileSystemObject");
  var filename = "c:\\Users\\Riccardo\\a.txt";
  var f = fso.OpenTextFile(filename, 2, true); 
  f.WriteLine(a);
  f.Close();
  }

this doesn't works, The script doesn't give me errors but the file is empty and The console shows the word: undefined! 这不起作用,脚本不会给我错误,但文件为空,控制台显示单词:undefined! Where is the problem? 问题出在哪儿? Thanks! 谢谢!

If I am understanding you correctly, what you are really asking is for a way to persist information in js between sessions. 如果我理解正确,那么您真正要问的是一种在会话之间将信息持久化在js中的方法。 I understand how a file looks great for that task. 我了解文件对于该任务的外观如何。 But since browsers have a very limited access to the filesystem, that is not ideal. 但是由于浏览器对文件系统的访问非常有限,因此不理想。

Instead of using a local file, you can persist the information you need in the browser's Web Storage. 您可以将所需的信息保留在浏览器的Web存储中,而不必使用本地文件。 You can come later and retrieve it. 您可以稍后再取回。 See http://www.html5rocks.com/en/features/storage for details 有关详细信息,请参见http://www.html5rocks.com/en/features/storage

Maybe it's some browser issue ? 也许是浏览器问题? I tried in IE9 - it works correctly (see jsfiddle - it's your code) 我在IE9中尝试过-它正常工作(请参阅jsfiddle-这是您的代码)

 var a = "Hello";
 WriteInFile(a);

http://jsfiddle.net/nb5N7/1/ http://jsfiddle.net/nb5N7/1/

But, also I must enable not safe activeX execution 但是,我还必须启用不安全的ActiveX执行

http://social.msdn.microsoft.com/Forums/vstudio/en-US/41b3a68b-dd16-48a8-b86c-02a1e543081c/activexobjectscriptingfilesystemobject-automation-server-cant-create-object (see answer) http://social.msdn.microsoft.com/Forums/vstudio/zh-CN/41b3a68b-dd16-48a8-b86c-02a1e543081c/activexobjectscriptingfilesystemobject-automation-server-cant-create-object (请参阅答案)

UPD: why you are minused me ? UPD:为什么你让我失望? UPD2: UPD2:

for ( var i = 0; row = refTab.rows[i]; i++) 

maybe possible issue here ? 也许这里有问题? you should write 你应该写

 var i = 0; i < refTab.rows.lenght; i++

instead of 代替

 for ( var i = 0; row = refTab.rows[i]; i++) 

and, also, try add alert(1); 并且,还尝试添加alert(1); to for cycle (to check is it work correctly) 到周期(检查它是否正常工作)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript如何创建和写入本地txt文件 - Javascript How to Create and Write to Local txt file 如何使用Javascript或jQuery但不使用Web服务器创建/写入本地txt文件 - How to create/write into a local txt file using Javascript or jQuery but no webserver 如何使用 javascript/jquery 将表单信息写入本地 txt 存档? - How to write the form information to a local txt archive using javascript/jquery? 如何用JavaScript写入txt文件 - How to write in txt file with JavaScript 如何使用JavaScript写入文件(.txt或.html) - How to write into a file (.txt or .html) in javascript 如何在Chrome上的javascript中读取客户端上的本地txt文件? - How to read local txt file on a client side in javascript on chrome? iMacro脚本-如何使用javascript读取本地.txt文件 - IMacro Scripting - How to read a local .txt file using javascript 是否可以使用 html 上的输入值保存文本并使用 JavaScript 写入本地 txt 文件? - Is it possible to save texts from input value on html and write on local txt file by using JavaScript? 如何使用Javascript文件夹中的文件名将数组写入TXT文件 - How to write to TXT file an array with filenames from folder in Javascript 您如何使用Javascript读取,写入和创建“ .txt”文件? - How can you read, write, and create “.txt” files using Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM