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:
Tutorials
- Clear cookie example
- JavaScript getElementById innerHTML
- JavaScript getElementById Style
- Javascript Examples
- JavaScript add row dynamically to table
- New Page 1
- JavaScript Change link
- JavaScript Checkbox getElementById
- javascript clear textarea
- JavaScript Clock Timer
- JavaScript Cookies
- JavaScript Date Difference
- JavaScript duplicate string
- JavaScript Email Validation
- javascript focus input
- JavaScript get excel file data
- JavaScript getAttribute Href
- JavaScript getAttribute Style
- JavaScript getElementById div
- JavaScript getElementById Iframe
- JavaScript getElementById select
- JavaScript Hide Button
- JavaScript Hide Div
- JavaScript hide image
- JavaScript Hide Table Column
- JavaScript Hide Table Rows
- JavaScript Key Event
- JavaScript link
- JavaScript method location
- JavaScript move div
- JavaScript move file
- JavaScript move image
- JavaScript Navigate Back
- JavaScript navigate to page
- JavaScript Navigate to URL
- JavaScript indexOf substring
- JavaScript onkeypress event
- JavaScript Open file
- JavaScript Open link in new window
- JavaScript Open Modal Window


