1

I am trying to make a simple script - writing something to a txt file when u press a button. I want to set it up on a localhost (full path - E:\xampp-portable\htdocs\test).

Currently stuck with this script:

<html>
<head>
<script language="javascript">
function Write()
{
var Scr = new ActiveXObject("Scripting.FileSystemObject");
var CTF = Scr.CreateTextFile("C:\\test.txt", true);
CTF.WriteLine('test');
CTF.Close();
}
</script>
</head>
<body onLoad="Write()">
</body>
</html>

It doesnt work tho. What path should i set here - var CTF = Scr.CreateTextFile("C:\\Gyan.txt", true);? I think this is where i make my mistake. Also, are there any other simple ways to just write something to a already existing txt file with javascript?

j08691
  • 204,283
  • 31
  • 260
  • 272
Edgar
  • 1,113
  • 3
  • 16
  • 30
  • "It doesnt work tho" — What does it do? What errors do you get? What is your test environment? – Quentin Apr 17 '13 at 19:54
  • @Quentin, it doesnt give me any erros or anything, it doesnt do anything at all, i think its because im giving it a wrong path.. – Edgar Apr 17 '13 at 19:54
  • What is your test environment? Where are you looking for error messages? – Quentin Apr 17 '13 at 19:55
  • In short, you can't. See this question: http://stackoverflow.com/questions/371875/local-file-access-with-javascript – ORION Apr 17 '13 at 19:53
  • This answer - http://stackoverflow.com/a/373512/319878 - mentions TiddlyWiki which has a file system abstraction for many platforms - http://trac.tiddlywiki.org/browser/Trunk/core/js/FileSystem.js. – Paul Grime Apr 17 '13 at 21:19
  • It almost looks like you should change your title - how to write to file system with ActiveX or something else appropriate. As you have your title at present, it does not reflect your presented "what I have so far" – Mark Schultheiss Apr 17 '13 at 21:20

1 Answers1

1

The JavaScript you have will possibly work with the Windows Scripting Host (of maybe with Internet Explorer, if you have very light security settings), but will not work in a browser for security reasons. Imagine visiting a website and automatically getting your desktop filled with advertisement PDFs... (at best)

What you can actually do with JavaScript in a browser is reading files, but they have to be specified by the used in a file input of a form.

Edit: look here about a way of doing this with Internet Explorer: http://4umi.com/web/javascript/filewrite.php (but it requires user interaction)

Ale
  • 1,727
  • 14
  • 26