0

I want to write a .txt file using the javascript console in IE. 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:

 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! Where is the problem? Thanks!

Martina
  • 1,852
  • 8
  • 41
  • 78
  • You can check this on SO http://stackoverflow.com/questions/11101641/activexobject-is-not-defined-and-cant-find-variable-activexobject – Ashoka Mondal Dec 21 '13 at 15:17

3 Answers3

1

Since HTML5 this is possible. See http://www.w3.org/TR/file-writer-api/#the-filesaver-interface and http://eligrey.com/blog/post/saving-generated-files-on-the-client-side

agrafix
  • 765
  • 1
  • 5
  • 15
  • 1
    It's not exactly the same, is it? The HTML5 method is for generating downloadable files. The OP tries to save the file directly to the hard drive without user interaction. – JJJ Dec 21 '13 at 15:23
  • True, I just wanted to point out that there actually are (crossbrowser) ways to write files in js. – agrafix Dec 21 '13 at 15:26
  • @MartinaF it's a way cleaner approach to offer the user to download the file, rather than writing it to disk. Otherwise you'd have to ensure that the directories exist... If you just need to persist some data between the pageloads, look at LocalStorage – agrafix Dec 21 '13 at 15:30
  • @agrafix I don't want to download the file... I want to write words in my existing file. (it's a personal use of the file) – Martina Dec 21 '13 at 15:31
  • How about collecting everything you need into LocalStorage and then Download/Save it to your desired location? – agrafix Dec 21 '13 at 15:33
  • @agrafix: in the future I have to create a batch that reads the data from hundreds of pages. So I don't want to download anything... – Martina Dec 21 '13 at 15:37
  • Then I can't see how can I help you, sorry. – haldagan Dec 21 '13 at 16:11
1

If I am understanding you correctly, what you are really asking is for a way to persist information in js between sessions. 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. You can come later and retrieve it. See http://www.html5rocks.com/en/features/storage for details

kikito
  • 51,734
  • 32
  • 149
  • 189
  • Hi, thanks for your answer! Is there any way to save in my filesystem this Web Storage? – Martina Dec 22 '13 at 13:12
  • Your web storage is an internal database in your browser. You can't save it to the regular filesystem, but you can still read it, show it on the website, and use it, even if you close the window and open a new one. – kikito Dec 25 '13 at 18:36
-1

Maybe it's some browser issue ? I tried in IE9 - it works correctly (see jsfiddle - it's your code)

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

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

But, also I must enable not safe activeX execution

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

UPD: why you are minused me ? 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); to for cycle (to check is it work correctly)

MaxStoun
  • 39
  • 4