<html>
<h2>Create Text file in JavaScript</h2>
<script>
function createFile(){
var object = new ActiveXObject("Scripting.FileSystemObject");
var file = object.CreateTextFile("C:\\Hello.txt", false);
file.WriteLine('Hello World');
file.WriteLine('Hope is a thing with feathers, that perches on the soul.');
file.Close();
}
</script>
<input type="Button" value="Create File" onClick='createFile()'>
</html>
This code doesn't work, it throws error
Uncaught ReferenceError: ActiveXObject is not defined
When opened in google chrome version 24.0.1312.52 m
Can anyone help on this.
Moto of this code is to create a text file with some content using javascript
When i try with Mozilla firefox with that add on firebug. It throws error as
ReferenceError: createFile is not defined
Could you please help me to modify the code so that it works, if the code is having problem.
My intention is create a webpage with some form and controls, once the user open the webpage (with the use of webpage file anywhere) and fills the data in the form and click submit button, a notepad file should be saved in his local drive under defined path. No client/server communication.
JavaScript cannot safe files on the client's computer. A nice tool to use is Downloadify ( https://github.com/dcneiner/Downloadify ). It's a little Flash application that you can easily style as a button. Using JavaScript you can feed it with a text and then by clicking on it it will allow the user to save the file.
Providing that the user allows it to by setting the security settings.
There is nothing wrong with the JS code as shown in that first post.
The error about "unterminated string constant" is simply not true.
So ssen must have changed the code before trying it with MSIE.
Now, having said all that...
The code will almost surely not run.
First of all, unless SSEN has changed the security settings in his MSIE browser, he won't be allowed to create the FileSystemObject.
Secondly, it is very unlikely that the browser will have permission to create a file in the C:\ directory. SSEN should be using some sub-directory that is used for *NOTHING* except junk file like this and that is marked as WRITABLE by the browser.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Yes, MSIE can – but who cares about MSIE only? Typically you wanna go for browser compatibility, don't you? Calling my statement "very wrong" is in fact very wrong as it is true in most (and more importantly, most relevant) cases.
I took it as him just copying something he found somewhere online without knowing that it would only work in MSIE. I know very few examples when something was developed for MSIE and MSIE only – all of which were professional enterprise solutions*.
I think it's not safe to infer from one line of code of a beginner that he wanted to go for a MSIE only solution. Even more so because he was using FireFox. Why would he wonder that it doesn't work in FF if he knew it's MSIE only?
*) Although you could argue that MSIE and "professional" are opposites.
You certainly are being childish ... rendering this discussion useless. The last time I checked Google Chrome wasn't the same as MSIE, so my point is still valid. Besides, it doesn't even matter – he never specifically asked for MSIE-only compatibility and I didn't see enough reason to infer it from his posting, so I provided a cross-browser solution.
Last edited by Airblader; 01-18-2013 at 10:12 PM..
Why do you insist on JavaScript? I gave you an alternative that is much more stable. For pure JavaScript to be able to do this cross-browser and reliably you will have to wait – indefinitely.