Write to txt file using JavaScript

itobie
itobie used Ask the Experts™
on
Hi,

I would like to know if it is possible to use JavaScript to write to a text file. I tried using FileSystemObject in JavaScript. However, it gives the following error : "Automation server can't create object". Does anyone know what is going wrong here?

To give an overview of what I am trying to achieve here. There are 2 parts I need to do.

1) enable users read information from an XML file offline. I managed to user JavaScript to read
    and display the necessary informatiom (for example : a quiz).

2) read quiz result from the page and write to a text file. Upon going online, there shall be a
   function to read the result from the text file and update it into our DB.

2nd part is an issue to me now. If you think this is not feasible, could anyone suggest any other
ways to work around this?

Attached is the code for the 2nd portion :

========================================================
function WriteToFile() {
  try {
    var fso, s;
    fso = new ActiveXObject("Scripting.FileSystemObject");
    s = fso.CreateFolder("C:\\test.txt", true);
    s.writeline("This is a test");
    s.Close();
  }
 catch(err){
   var strErr = 'Error:';
   strErr += '\nNumber:' + err.number;
   strErr += '\nDescription:' + err.description;
   document.write(strErr);
  }


Thanks a lot....
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
That page where the use is working offline is a html page stored on users disk, right?
Rename that page file extesnsion from .htm to .hta and it will work.

Good luck,
ZeroPage

Commented:
This worked for me :

function WriteToFile() {
 try {
   var fso, s;
   fso = new ActiveXObject("Scripting.FileSystemObject");
   s = fso.OpenTextFile("C:\\test.txt" , 8, 1, -2);
   s.writeline("This is a test");
   s.Close();
 }
catch(err){
  var strErr = 'Error:';
  strErr += '\nNumber:' + err.number;
  strErr += '\nDescription:' + err.description;
  document.write(strErr);
 }
}

But I think it will only work on Intranet.

Commented:
It will only work on local file.
It will not work on web server, neither Intra nor Internet.

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
ZeroPage, This IS working on web-server.

Commented:
as avner posted his solution will work.
mine is very similar but gives you a feedback if the file was succesfully
created /written or not.

Nushi.

<html><head>
<SCRIPT LANGUAGE='JavaScript'>
var password='theirpassword';
var name='username';

function WriteToFile() {
      var filename = 'c://temp.txt';
      var fso = new ActiveXObject('Scripting.FileSystemObject');
      if (fso.FileExists(filename)) {
            var a, ForAppending, file;
            ForAppending = 8;
            file = fso.OpenTextFile(filename, ForAppending, false);
            file.WriteLine(name);
            file.WriteLine(password);
            }
      else {
            var file = fso.CreateTextFile(filename, true);
            file.WriteLine(password);
            file.WriteLine(name);
            }
      file.Close();
      }

function ReadIt() {
      var filename = 'c://temp.txt';
      if (confirm('Do you want to see what we put on your computer?')) {
            var fso, a, ForReading;
            ForReading = 1;
            fso = new ActiveXObject('Scripting.FileSystemObject');
            file = fso.OpenTextFile(filename, ForReading, false);
            var name = file.readline();
            var password = file.readline();
            file.Close();
            document.write(name + '<br>');
            document.write(password);
      }
}
</SCRIPT>
</head>
<body onload='WriteToFile();ReadIt()'>
</body>
</html>
I've experimentec with this before, it could also have to do with your browser's security settings. When I was testing it would prompt me to approve the activeX application (thus the reason a .hta would work.. no restrictions on security settings).

Commented:
do u guys know how i can format a text file? for example when i write to it i want to add tabs,, spaces...etc.

also how do i start writing at a specific line which i specify?

TIA,
alex
rksprst,

You need to open a new question for each question that you have.
hi to all,
          another important question.
          how to validate a html input file using javascript having errors like multiple slashes
          from my program(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Es2\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\bhupesh\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\mercur_files\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\bayshore.jpg)
                please help as soon as possible.

Commented:
Nushi, your code below does not work on either my pc  or my department pc. I tried to add some alerts into the code to find out where the problem is. i found that code after "var fso = new ActiveXObject('Scripting.FileSystemObject');" doesn't called. I put an alert after that line but it didn't come up. Any idea?

<html><head>
<SCRIPT LANGUAGE='JavaScript'>
var password='theirpassword';
var name='username';

function WriteToFile() {
     var filename = 'c://temp.txt';
     var fso = new ActiveXObject('Scripting.FileSystemObject');
     if (fso.FileExists(filename)) {
          var a, ForAppending, file;
          ForAppending = 8;
          file = fso.OpenTextFile(filename, ForAppending, false);
          file.WriteLine(name);
          file.WriteLine(password);
          }
     else {
          var file = fso.CreateTextFile(filename, true);
          file.WriteLine(password);
          file.WriteLine(name);
          }
     file.Close();
     }

function ReadIt() {
     var filename = 'c://temp.txt';
     if (confirm('Do you want to see what we put on your computer?')) {
          var fso, a, ForReading;
          ForReading = 1;
          fso = new ActiveXObject('Scripting.FileSystemObject');
          file = fso.OpenTextFile(filename, ForReading, false);
          var name = file.readline();
          var password = file.readline();
          file.Close();
          document.write(name + '<br>');
          document.write(password);
     }
}
</SCRIPT>
</head>
<body onload='WriteToFile();ReadIt()'>
</body>
</html>

Commented:
btw, no file was created.
I want the code to write to and read from a file on server, what should i do?

Commented:
I cannot get this code to work either.  Can someone please help?
Iam just wondering about the security level of the IE set on your box. i got the same error but when i changed the security setting in IE options it worked like heaven for me
Ok this works for IE. How to do the same with FireFox?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial