3

Is it possible in javaScript to create an html file in same directory ? After some searching on google I landed upon using ActiveXObject like this :-

var fso = new ActiveXObject("Scripting.FileSystemObject");
        varFileObject = fso.OpenTextFile("C:\apache-tomcat-7.0.23\webapps\proof\web.html", 2, true,0);
        varFileObject.write(data);
        varFileObject.close();

But it is showing an error which is ActiveXObject is not defined or does not exist. I am not sure why this error is coming. Is this code browser specific? I am using chrome and I need to write html file using javascript. Please help me what can i modify in above code so that it can run. Or help me with some other alternative.

Steve Claridge
  • 10,650
  • 8
  • 33
  • 35
Lina Clark
  • 243
  • 4
  • 9
  • 1
    TiddlyWiki does local file I/O this way - http://oldwiki.tiddlywiki.org/wiki/How_To/Configure_your_browser_to_allow_saves_to_disk – Paul Grime Apr 03 '12 at 08:33
  • ActiveXObject is IE speciifc. – Pranav Apr 03 '12 at 08:34
  • 1
    may be this could help you:- http://stackoverflow.com/questions/292566/browser-application-local-file-system-access – Pranav Apr 03 '12 at 08:38
  • 1
    I was just thinking if javascript is allowed to make text files, then isn't it a security risk because then any website is able to store some kind of file on users' machine. I have not read other techniques Just a random thought – Lina Clark Apr 03 '12 at 08:51

2 Answers2

3

ActiveX Object works only in Internet Explorer. And browser is not allowed to make any changes to your file system. However if you need to save html page, then you have to do some extra things like send the url of html page or whole page as string to a server, host the server on localhost using tomcat and you can use java code to store the fetched url or string sent by you on your machine. I have done the same. If you need I can provide you the code too. (btw code is quite easy). To fetch the page using URL you can use the Jsoup library which is very good html parser. Hope this helps!

Sachin Jain
  • 21,353
  • 33
  • 103
  • 168
3

ActiveXObject is a IE only thing and even then the user is prompted if they want to use it or not and susbequently warned of the dangers.

The browser isn't given access to the filesystem, for good reason. Imagine if any old website could write files to your disk!

Evan Trimboli
  • 29,900
  • 6
  • 45
  • 66