Write to txt file using JavaScript

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....
itobieAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
avnerCommented:
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.
ZeroPageCommented:
It will only work on local file.
It will not work on web server, neither Intra nor Internet.

Determine the Perfect Price for Your IT Services

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

avnerCommented:
ZeroPage, This IS working on web-server.
NushiCommented:
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>
TheKenmanCommented:
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).
rksprstCommented:
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
TheKenmanCommented:
rksprst,

You need to open a new question for each question that you have.
bhubesh_beCommented:
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.
riekeyCommented:
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>
riekeyCommented:
btw, no file was created.
I want the code to write to and read from a file on server, what should i do?
sammeCommented:
I cannot get this code to work either.  Can someone please help?
sasisumanCommented:
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
dolbyarunCommented:
Ok this works for IE. How to do the same with FireFox?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.