Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

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

irt.org | Knowledge Base | JavaScript | File | Q1421 [ previous next ]

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

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?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.