Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write to an external file when running JS under Linux?

Status
Not open for further replies.

caddie

MIS
Jul 10, 2003
2
US
Hi,

The following is a javascript code snippet to write to a file.

------------------------------------------------------
var fso, t;
fso = new ActiveXObject("Scripting.FileSystemObject");
t = fso.CreateFolder("C:\\file.txt", true);
t.writeline("Testing");
t.Close();
-------------------------------------------------------

However this is for Windows platform. Does anyone know if there is an equivalent method using Linux?

Thanks for your help.
 
It's not a "Windows thing", it's an "Internet Explorer thing". It wouldn't work under Firefox, Opera, or Safari for Windows.

While you can use some java.io.File.* routines in Firefox (e.g. to find the size of a file), things like writing to a file would (as far as I know) be restricted for security reasons.

It's possibe that if you were using a signed script of some sort, then it would work. Take a read here for more on these:
Just in case you do go down this route, try something like this:

Code:
var theFilename = 'C:\\file.txt';
var theData = 'Testing';

if (window.ActiveXObject) {
	try {
		var fileObj = new ActiveXObject('Scripting.FileSystemObject');
		var t = fileObj.CreateTextFile(theFilename, true);
		t.WriteLine(theData);
		t.Close();
	} catch(e) {
		alert('There was an error writing your file.\n\n' + e);
	}
} else {
	try {
		var fileObj = new java.io.PrintWriter(new java.io.FileWriter(theFilename));
		fileObj.println(theData);
		fileObj.close();
	} catch(e) {
		alert('There was an error writing your file.\n\n' + e);
	}
}

I've not tested it, but you never know - it might just work!

Incidentally, I switched your usage of "CreateFolder" for "CreateTextFile", and "writeline" for "WriteLine".

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top