PDA

View Full Version : Can you open, modify, and close a txt document using JavaScript?



kasei
10-21-2006, 12:34 PM
Hi,

Is there a script that will open a text file, write some info into it, and then close it?

Basicly I'm looking for a JavaScript alternative to fopen, fwrite, and fclose in PHP.

Thanks!

Joe

Twey
10-21-2006, 12:53 PM
No, of course not. A site that can read and modify the user's files?

You can use cookies via document.cookie, but that's as close as you're getting.

kasei
10-22-2006, 02:30 AM
What I'm looking for is something like this:

<SCRIPT LANGUAGE="JavaScript">
<!--
var ForAppending = 8;
var TristateTrue = -1;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var newLine = fso.OpenTextFile("C:/test/database.txt", ForAppending, true, TristateTrue);
newLine.Write("Hello World!\n")
newLine.Close
-->
</SCRIPT>
But why isn't each entry on it's own line? If I run the script a few times it looks like
Hello World!Hello World!Hello World!
when I view it in Notepad but it looks like
Hello World!
Hello World!
Hello World!
when I view it in IE or paste it into a Word document.

Thanks,

Joe

djr33
10-22-2006, 02:46 AM
ActiveX can do that. That and Java. (Note: Java isn't Javascript. Think applets.)
PHP, and other server side codes, can likewise edit things on the server.

ActiveX isn't javascript. It's a part of Javascript, kinda, but it's Internet Explorer only, and, as you can tell from what you're doing, it is a security risk in some ways.

As for just adding a new line, I would assume that \n would work. However, you might want to add a carriage return character as well: \r\n
Not sure if that will help, and ActiveX isn't my thing, so can't tell you much more.

kasei
10-22-2006, 03:26 AM
Thanks.

It is actually putting the line breaks in there, they just don't show up when I view the file with Notepad. I probably have to save the text file in a different format or something. I guess it doesn't really matter, as long as the script can see the line breaks! ;)

do you know how to turn off that annoying activex security warning every time the script runs?

Thanks,

Joe

the-disturbed
10-22-2006, 03:35 AM
activeX settings like that are client side, obviuosly nothing can be done on the server side to bypass these settings, other wise the world would be overrun by viruses, that eventually mutate into something that'll tear your head off when you get to close to the computer LMAO

LMAO sry im high on caffeen, had a morbid moment LMAO

kasei
10-22-2006, 04:11 AM
That's right.

I'm trying to write a little script that will append or modify a database on my desktop. It's just that the alert is annoying every time I run the script so I'd like to turn it off, though I do worry about opening myself up to a virus attack.

Oh well, thank you everyone who replied.

Joe

Twey
10-22-2006, 11:59 AM
the-disturbed: :eek:

The reason the linebreaks aren't visible in Notepad is that you're using a UNIX-style linebreak (\n) instead of a Windows-style linebreak (\r\n), and Notepad is too stupid to work it out.

djr33
10-22-2006, 11:20 PM
kasei, I'm sure there's something, probably in advanced settings, to turn off the warning popup in Internet Explorer. We thought you meant disabling the warning through the script for anyone who viewed the page. It's a local setting, so the script can't do that, but the user can disable it if they want.

Twey, I mentioned \r\n above... not sure if kasei tried it or not, but s/he said it didn't work in notepad. Might there be another reason?
And, good to know unix vs. windows. Does \r do anything to unix? (Rather, would it be invalid to use that just in case, so it works on windows too?)

Twey
10-22-2006, 11:35 PM
UNIX editors won't like \r\n very much, but they're usually smart enough to pick up on it and convert it accordingly. It's pretty safe to use except for sensitive things like shell scripts.

djr33
10-23-2006, 12:54 AM
But .txt's, for example would be fine, I take it.
Makes sense.

the-disturbed
10-23-2006, 01:24 AM
Notepad is too stupid to work it out.

i thought notepad was just a bit by bit reader, that'll see anything

tech_support
10-23-2006, 07:23 AM
i thought notepad was just a bit by bit reader, that'll see anything

...that crashes each time you load a big file :)

Twey
10-23-2006, 10:35 AM
i thought notepad was just a bit by bit reader, that'll see anythingSure it is, but how it displays that is still variable.

Open a file with old Mac-style linebreaks (\r) in kedit, for example, and it'll pick up on the fact that the file isn't using \n and treat \r as the linebreak instead. Open that same file with Notepad, and it'll not only not treat \r as a linebreak, but it won't even insert the generic "unknown character" symbol, IIRC.