1

I have the following script:

document.open("text","c:\\path\\to\\file\\browser.txt");

appendChild(navigator.appCodeName + ";");
appendChild(navigator.appName + ";");
appendChild(navigator.appVersion + ";");
appendChild(navigator.userAgent + ";");
appendChild(navigator.platform + ";");
appendChild(is_java + ";");

// ----------------------

if (is_opera) {
     appendChild("<TT>***Version numbers here are only valid</TT><BR>");
     appendChild("<TT>***if Opera is set to identify itself as Opera</TT><BR>");
     appendChild("<TT>***use is_opera vars instead</TT><BR>");
     }
     appendChild(is_major + ";");
     appendChild(is_minor + ";");

// ----------------------

if (is_opera) {
     if (is_opera7up) {
     appendChild(is_js);
         } else {
         appendChild(is_js);
         }
             } else {
             appendChild(is_js + ";");
             }

// ----------------------

appendChild(is_screen + ";");
if (window.screen) {
     appendChild(screen.height + ";");
     appendChild(screen.width + ";");
     appendChild(screen.availHeight + ";");
     appendChild(screen.availWidth + ";");
     appendChild(screen.colorDepth + ";");
}

// ----------------------

if (is_Flash) {
     appendChild(is_FlashVersion);
     } else {
     appendChild("unknown");
     }

document.close();

It does what I want it to do by collecting browser information. HOWEVER, it is not writing information to the file. How do I make it append the browser information to the text file?

You see the appendChild -- I also tried using document.write (which puts the information on the screen).

I am a Javascript newbie so please make your answers as straightforward as possible. Thank you.

Mr_Thomas
  • 857
  • 3
  • 19
  • 39
  • You cannot simply create an arbitrary file via JavaScript hosted in a web browser. What is your rationale/end goal for this? – Alex K. Aug 10 '15 at 16:29
  • Use console.log() for such debugging purposes – Matthias Aug 10 '15 at 16:31
  • shouldn't you be storing a reference to the document you opened? as in `var myDoc = document.open("text","c:\\path\\to\\file\\browser.txt"); myDoc.appendChild(navigator.appCodeName + ";");` ... – RAEC Aug 10 '15 at 16:32
  • What is your `appendChild()` function? What does it try to do? Where's the code for it? – jfriend00 Aug 10 '15 at 16:35
  • @AlexK.: I'm just trying to log some browser information for users of a local intranet. – Mr_Thomas Aug 10 '15 at 16:35
  • @Matthias: is that all I have to do... change the `appendChild` to `console.log()`? – Mr_Thomas Aug 10 '15 at 16:36
  • @jfriend00: Honestly, I don't know. I found a collection of Javascript files and just mashed them together (please don't hate me). Like I said, it does what I want it to do -- it just doesn't output the file where I want it. – Mr_Thomas Aug 10 '15 at 16:37
  • Seriously? You're using a function and you don't know what it does and you can't put the code into your question? You need to at least learn enough about the code you're using to do that. That is part of the minimum requirement to ask questions here. We don't mind that you don't know something. We do mind if you can't find a function in your own code that we're asking to see and include it in your question. That's required here in order to ask clear questions that include the necessary information to provide a meaningful and complete answer. – jfriend00 Aug 10 '15 at 16:38
  • possible duplicate of [Is it possible to write to a file (on a disk) using JavaScript?](http://stackoverflow.com/questions/461791/is-it-possible-to-write-to-a-file-on-a-disk-using-javascript) – Ghazgkull Aug 10 '15 at 17:41

3 Answers3

1

You can not do this. Beacuse browser can not write file.

You can use node.js https://nodejs.org/api/fs.html

fielfdjf
  • 59
  • 3
0

You can generate a file for download. Check the solutions here:

Javascript: Create and save file, and Using HTML5/Javascript to generate and save a file

Community
  • 1
  • 1
Tolbxela
  • 4,767
  • 3
  • 21
  • 42
-2

This is a very common newbie Javascript question that you could answer for yourself with a quick Google search. Web browsers will not let you write files to the local filesystem.

(This question should be closed as a dup. For example: Is it possible to write to a file (on a disk) using JavaScript?)

Community
  • 1
  • 1
Ghazgkull
  • 970
  • 1
  • 6
  • 17
  • 1
    I did a Google search but it seemed all that I was getting was Vbscript and ASP examples which, obviously, I don't want. – Mr_Thomas Aug 10 '15 at 16:51
  • Okay. Well, the answer is "No". The browsers won't let Javascript write files to the local filesystem because it would be a security problem. Imagine the damage a malicious website could do if they could write files to disk. – Ghazgkull Aug 10 '15 at 16:53