0

So I am currently having an issue where I have a button set up in HTML that checks which of series of check boxes are checked through a JavaScript function. Based on which of these check boxes are checked the function writes numbers (ex: 1.2,...3.4, etc) to a variable. I would like to get the contents of that variable into a text file but after some research it appears that JavaScript cannot write to a file. I was wondering if anyone has some means of getting the contents of this variable into a place where I can send it to a text file like php?

  • The simplest way would probably be to post the data to a page that *can* write to a file -- that is, a PHP or ASP/ASPX (or similar) page that runs code on the server and can access its resources. But you won't be able to create a text file on the client, if that's what you're trying to do. – Bond Apr 03 '14 at 19:56
  • Technically speaking, if the Javascript saves the data into a cookie, you're saving the file locally. :) – Sunny Patel Apr 03 '14 at 21:04
  • Javascript *can* write to a text file, just not not within a regular web browser. IF you are running under Windows, *and* you are running the file locally off your hard drive you can use [HTA](http://en.wikipedia.org/wiki/HTML_Application). See [this answer](http://stackoverflow.com/questions/14795357/javascript-in-html-write-to-file/14796133#14796133) for details. – Jeremy J Starcher Apr 04 '14 at 04:57

1 Answers1

0

Javascript is code that runs on the client. You can have the Javascript (or a form!) generate a request to the server to create a file on the server with the necessary text you want in the file. You can then request for that file from the server. The server code will have to handle all of this, whether you use Java, PHP, ASP, etc...

If you just want text from a variable, you can setup the Javascript to simply alert( variable );, or if you want to be less intrusive, display it in your Console: console.log( variable );

Sunny Patel
  • 7,830
  • 2
  • 31
  • 46