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?

6
  • In short, you can't. See this question: stackoverflow.com/questions/371875/… Commented Apr 17, 2013 at 19:53
  • "It doesnt work tho" — What does it do? What errors do you get? What is your test environment? Commented Apr 17, 2013 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.. Commented Apr 17, 2013 at 19:54
  • What is your test environment? Where are you looking for error messages? Commented Apr 17, 2013 at 19:55
  • This answer - stackoverflow.com/a/373512/319878 - mentions TiddlyWiki which has a file system abstraction for many platforms - trac.tiddlywiki.org/browser/Trunk/core/js/FileSystem.js. Commented Apr 17, 2013 at 21:19

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.