Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1421 How can I read and write to a text file?

You are here: irt.org | FAQ | JavaScript | File | Q1421 [ previous next ]

JavaScript cannot read or write to files. However, it is possible to do this using ActiveX embedded into your JavaScript

//-------------------------------------------------------
function WriteToFile()
{
 var filename = "data.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(password);
 }
 file.Close();
}
//-------------------------------------------------------
function ReadFromFile()
{
 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();
}
//-------------------------------------------------------

The windows scripting host has first to be installed on the client.

In Netscape, signed JavaScript using LiveConnect can create Java input/output streams and read and write to files too if the user grants UniversalFileAccess.

Submitted by James Luff

Feedback on 'Q1421 How can I read and write to a text file?'

©2018 Martin Webb