2

I am trying to create a json file from javascript . since i didnt get the code i am trying to create a text file as ..

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script language="javascript">
function textFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("D:\\Test.txt", true);
s.WriteLine('Hello JavaScript');
s.Close();
}
</script>
</head>
<body onLoad="textFile()">
</body>
</html>

This code works only in IE ... please help me to work this code at least in chrome.. Please help me to create json file also...

ess
  • 663
  • 3
  • 9
  • 17
  • you would need to use the filesystem api. Here's a [tutorial](http://www.noupe.com/webdev/html5-filesystem-api-create-files-store-locally-using-javascript-webkit.html), but not all browsers support it yet. You could also use [local storage](http://diveintohtml5.info/storage.html) but again older browsers do not support it for instance ie<8.0, and it has a limit on how much can be stored – Patrick Evans Jan 25 '14 at 05:32
  • Please not that there is nothing in your example related to JSON. – Felix Kling Jan 25 '14 at 05:33
  • thanks for the comment... i want to create a file ...not download it. i am assuming that if i can create a text file it is easy to create a json file.. thats why i didnt mention any json code.. – ess Jan 25 '14 at 05:41
  • Do you really want arbitrary web sites to be able to create files on your computer? – user229044 Jan 25 '14 at 05:51
  • no..actually i didnt understand your qn..("Do you really want arbitrary web sites to be able to create files on your computer?") – ess Jan 25 '14 at 05:56

2 Answers2

2

@ess, a modern web browser, at least the ones 99% of the world will use to access your web site, will not allow you to securely create a file from the web page.

There are hacks and workarounds to this here or there, such as ActiveX as above, but for example this will only possibly work in internet explorer.

You are using the wrong tool for the job. This isn't what a web browser is for. It's the job of a native language, or server-side Javascript. Perhaps you can explain a bit clearer what you are trying to do with this file?


In case I am misunderstanding you, If you are simply trying to create a JSON object and use it in your page, this is much simpler:

var jsonObject = {
  foo: 'bar'
};

alert(jsonObject.foo);
dthree
  • 19,847
  • 14
  • 77
  • 106
0

ActiveX is a Microsoft only feature. It would be nice if all of the browsers agreed on something or adopted ActiveX--don't hold your breath....

So if you really, really want your code to run in Chrome, try the ActiveX for Chrome extension. It is hit or miss...it works well for most AcitveX controls...some not so well.

https://chrome.google.com/webstore/detail/activex-for-chrome/lgllffgicojgllpmdbemgglaponefajn?hl=en

It is a bit strange to setup and use. When you go to your page the first time, the extension will ask if you want to use it. You may also need to go into Chromes settings/extensions and tweek the settings.

Brian McGinity
  • 5,777
  • 5
  • 36
  • 46