I've made an empty mynote.txt in the same directory,
and open the following code with Internet Explorer,
and allow to write on hard disk (when IE asked)
but nothing in the text file: mynote.txt after the run.
what is the problem?

<html>
<body>
       
<script type="text/javascript">
       
function WriteFile()
{
  var fh = fopen("\\mynote.txt", 3);
    
  if(fh!=-1)
  {
    var str = "Some text goes here...";
    fwrite(fh, str);
    fclose(fh);
  }
}

WriteFile();

</script>

</body>
</html>

Dani AI

Generated

Short answer for : browser JavaScript running in a normal webpage does not provide fopen/fwrite-style access to arbitrary local file paths, so the calls you tried simply don’t exist in that environment. Browsers only let pages read files the user explicitly selects (File API / FileReader) and they restrict write access for security. (developer.mozilla.org)

About ’s suggestion: yes, on old Internet Explorer installs you can reach the OS via COM/ActiveX (Scripting.FileSystemObject) from script — that will let a page write files, but it’s an IE-only, privileged mechanism that requires the user or admin to allow ActiveX (and that’s risky). Internet Explorer has been retired/disabled in many environments, so relying on ActiveX is not a forward-looking solution. (frost.cs.uchicago.edu)

Better, practical options today:

  • Use the File System Access API (user picks where to save) for modern Chromium-based browsers. It requires HTTPS and a user gesture, but it gives a straightforward save flow. Example (modern browsers):

    async function saveText(filename, text) {
      const handle = await window.showSaveFilePicker({
        suggestedName: filename,
        types: [{ description: 'Text', accept: { 'text/plain': ['.txt'] } }]
      });
      const writable = await handle.createWritable();
      await writable.write(text);
      await writable.close();
    }

    (developer.mozilla.org)

  • Cross‑browser fallback: create a Blob and trigger a download (works broadly) or send the data to your server (AJAX) and write files server-side with Node/PHP if the file must be written without user interaction. Example (Blob + download):

    const blob = new Blob([text], { type: 'text/plain' });
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url; a.download = 'mynote.txt';
    a.click();
    URL.revokeObjectURL(url);

    (developer.mozilla.org)

Troubleshooting notes: avoid trying to force browser scripts to write arbitrary paths (security prevents this). If targeting internal enterprise machines where IE/ActiveX is still used, document the security implications and prefer an alternate workflow (file picker, server endpoint, or a signed desktop app).

Recommended Answers

All 5 Replies

As far as I know you can't write to a file in this method from within a web page. You need to create an ActiveXObject File System Object in order to access the local file system. This will also only be available in IE as other browsers don't allow access to the FS via web code (rightfully imho) due to security concerns.

function WriteFile()
{
  var fso = new ActiveXObject("Scripting.FileSystemObject");
  var ots=fso.OpenTextFile("c:\\mynote.txt",2,true);
  
  var str = "Some text goes here...";
  ots.Write(str);
  ots.Close();
  ots=null;
  fso=null;
}
WriteFile();

I used IE for opening the javascript, as I mentioned.

Did you use the ActiveXObject and did it work?

I dont know. Wht is it? And how can I check? Its a software?

It's the example code I provided in my previous posting....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.