Take the 2-minute tour ×

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

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!

share|improve this question
You can check this on SO stackoverflow.com/questions/11101641/… –  Bikash Chandra Mondal Dec 21 '13 at 15:17

3 Answers 3

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

share|improve this answer
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. –  Juhana 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
question edited –  MartinaF Dec 21 '13 at 15:29
@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) –  MartinaF Dec 21 '13 at 15:31

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

share|improve this answer
Hi, thanks for your answer! Is there any way to save in my filesystem this Web Storage? –  MartinaF 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

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

 var a = "Hello";


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:


maybe possible issue here ? you should write

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

instead of


and, also, try add alert(1); to for cycle (to check is it work correctly)

share|improve this answer
I don't minused you. –  MartinaF Dec 21 '13 at 15:33
I mean not exactly you –  MaxStoun Dec 21 '13 at 15:36

Your Answer


