A very common question is whether or not it is really possible to write to a text file using JavaScript. After much research was conducted, it has been claimed that we cannot do so, as there is no permission for JavaScript to write to the hard drive in the client machine. It is actually not a good idea 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 browsers (IE or FireFox or Chrome) in client’s workstation, if you wish to store data at server side, it is just not possible with JavaScript. In that case, 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. We Know this technology by the name "Ajax".
Using client side ActiveX technology:
However, if you want it for your internal purpose or have full faith on your client you can very well do this using ActiveX or MSIE 6, which is running on Microsoft Windows. If you want to store data on the client side, then this guide will really help you out. 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.