html JavaScript How to read and write files in JavaScript


  • Forum Categories
  • laptop
    Hardware and Software

  • code

  • live_tv
    Digital Media

  • local_cafe
    Community Center

  • Activities
  • Donate with PayPal
  • forumLatest Posts
  • Member Top List
  • DaniWeb Ads
  • Forum API Docs
  • Community Rules
  • About Us
  • Contact Us
© 2018 DaniWeb® LLC

save data to .txt file using javascript

Member Avatar

Hi everybody,

I need to save same data to a .txt file (using javascript).
Please see this example of clicking in an image and get the coordinates ( Click Here ). The coordinates appear on the bottom of the image. What I need is to save this information (x and y coordinates) to a .txt file.
I read about it and tried some code but i couldn’t found any solution. Not even write ou create a .txt file.
I tried this peace of code just to create a file but I have a syntax error on line 6. This is just an example of code available on the internet. I tried a lot of other examples but i couldnt find nothing that creates the .txt file.
And I use both Safari and Internet Explorer.

<script language="javascript">
function WriteToFile()
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("C:\\Test.txt", true);
<body onLoad="WriteToFile()">

Anyone know how to do this? Or have some idea how to solve it?
Im trying to use javascript but perhaps the solution is php. I dont know.
Thanks for the help.


  • 6 Contributors
  • forum7 Replies
  • 6 Years Discussion Span
  • comment
    Latest Post

    by Troy III

Member Avatar

I didn’t think JavaScript running in a browser had access to the file system of a computer, as this would be a pretty huge security flaw.

You could use PHP to create a text file on the server for the user to download.

Member Avatar

That script you copied isn’t going to work in a browser. It could be used within WSH (windows scripting host)

Member Avatar

Thank you both for your replies.
As you said blocblue, its possible to do that using PHP.
Easy to create and save a .txt file.

Thank you

Member Avatar

i use from code you and see Error
automation servsr can not ccreate object

Member Avatar

That page should be run on IE – after running it you should get a safety warning msgBox : hit YES than check in “C:/” and get “test.txt” file for your hello message. It will be there.


by Troy III

Member Avatar

Not to beat a horse that’s been decaying for the past two years but this sounds like a job for Node.js (trumpets blazing)

See the documentation on File System:

var fs = require('fs');
fs.readFile('/etc/passwd', function (err, data) if (err) throw err; console.log(data);

Member Avatar

The case in question is about obtaining a horse first, than – if you like – you may also beat it.