How to Write to a Text File using JavaScript

Page content

Storing Small Text

A very common question is whether or not it is really possible to write to a text file using JavaScript. Well, it’s not possible to write a text file, as JavaScripts don’t have permission to write to the hard drive in the client’s machine. However, If the amount of text you want to store is not too big, you may try to use cookies.

It is actually not a good idea to try to store text file using JS, as this could allow one to access local file systems of your computer or visitor’s website; this is indeed a serious security issue.

Using Ajax or Server Side Language

Since JavaScript is a client side scripting language that is executed by web browsers in client’s workstation, it’s not possible to store data at server side, using JavaScript.

Hence, you can never write to a text file with JavaScript, instead you may use ASP, PHP, PEARL or any other server side language with JavaScript to make it work.

Since JavaScript is not allowed to write to the file system of the server, so you must first ensure that there is something running on the server, which has the permission to do so. Rhino is another solution to this, although very few of us use this.

You can also use XMLHttpRequest Object of JavaScript and send the data entered by the user to the server. You do not have to leave your current page, and once it reaches the server the server-side script will look after the rest of the part; this technology is better known as “Ajax”.

Using Client Side ActiveX Technology

If you want to do so for your internal purpose or have full faith on your client you can very well do this using ActiveX scripts.

But, this technique will only be useful if the client machine supports ActiveX. Scripting.FileSystemObject is an ActiveXObject, which has the function CreateTextFile(parameter1, parameter2) to write to the file system of the machine where the Javascript will be executed.

CreateTextFile() creates a file as specified in the first parameter and returns a TextStream object that actually writes to the specified file. The second parameter here accepts a Boolean value. If its value is true that means the file can be overwritten if it exists.

To write any content to the file WriteLine(“Text to write”) method of the TextStream object is used. To make the content permanent the file is closed using Close() method. This way we can write a file to store any text data only.

Here’s a sample code snippet - (please remove all the whitespace, when you try to run it!)

< span onClick= " file() " >Hello < /span > < /p >

< SCRIPT LANGUAGE = " JavaScript " >

function file()


var ForAppending = 8;

var TriStateFalse = 0;

var fso = new ActiveXObject(“Scripting.FileSystemObject”);

var newFile = fso.OpenTextFile(“c:\\WINDOWS\\Desktop\\001122.html”,

ForWriting, true, TriStateFalse);



Should You Really Do It?

As discussed in the above two sections, there are few ways to write to a text file using JavaScript, but you should think twice before actually doing it, due to the securtiy issues involved.

Secondly, you must enable ActiveXScripts on your PC for the above code snippets to execute properly. At the same time, it’s important to understand that you should only allow ActiveXScripts from reliable sources to be executed on your PC to avoid any malicious attacks.