Home Javascript Javascriptexamples JavaScript write to text file
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

JavaScript write to text file

Advertisement
This page discusses - JavaScript write to text file

JavaScript write to text file

       

In this section, we are going to create a file and write text into it using JavaScript.

In the given example, firstly we have created an ActiveXObject object which is used to enable and return a reference to an Automation object and  used it with the CreateTextFile(), a JavaScript method , which generates a file specified and returns a TextStream object to read from or write to the file. The Boolean value defined in this method indicates whether to overwrite an existing file. Then, in order to write the text into the created file, we have used WriteLine() method. This code works only on Internet Explorer.

Syntax of ActiveXObject object:

var newObject = new ActiveXObject(servername.typename[, location])

The Active object takes three parts:
servername
-  the name of the application providing the object. It is required.
typename -  the type or class of the object to create.
location - the name of the network server where the object is to be created.

 Here, we have taken servername as 'Scripting' and typename as 'FileSystemObject':

Here is the code:

<html>
<h2>Create Text file in JavaScript</h2>
<script>
function createFile(){
var object = new ActiveXObject("Scripting.FileSystemObject");
var file = object.CreateTextFile("C:\\Hello.txt", false);
file.WriteLine('Hello World');
file.WriteLine('Hope is a thing with feathers, that perches on the soul.'); 
file.Close();
}
</script>
<input type="Button" value="Create File" onClick='createFile()'>
</html>

Output will be displayed as:

Download Source Code:

       

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 18, 2011

Related Tutorials

Ask Questions?    Discuss: JavaScript write to text file  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Pierre
June 8, 2011
JavaScrit write to text file

The JavaScript write to text file doesn't work ! I think this is for security reason, but i must create one ascii file in the client side. HELP PLEASE
Arun kumar
June 22, 2011
reply for ur content.....

Sir, your tutorial was very useful....please continue your services....it will be useful to most of them...
Hash
May 9, 2012
Changing the Name

How do you change the name of the file, for example i'm using this to create log files but i'm not sure how to change the name..any suggestions
Priya
June 23, 2012
ActiveXObject is not defined

I tried this example the Firefox has the following error:ActiveXObject is not defined!
Navneeta
August 10, 2012
What object to use in case of browsers like Safari

The solution you provided works in IE8+, but not in Safari. Could you suggest a solution in there?
Scanner
December 24, 2012
isnt working

i use windows 7 with UAC :??? i know that C:\\users\user\desktop does not want UAC any suggest? //http://www.roseindia.net/javascript/javascriptexamples/javascript-write-to-text-file.shtml
dinesh
June 25, 2013
create file cilent side

how to create text file to cilent side..in javascript
Mukesh Kumar
July 11, 2013
YES

YES IT WORKED JUST CHANGE THE DIRECTORY FROM C TO OTHER BECAUSE IT REQUIRES ADMINISTRATOR PERMISSION.
DMCA.com