8

I have been trying to do this with no luck so I've decided to ask SO.

I have a page with a bunch of different buttons and each button has it own parameters.

I want to create a .txt file as a log and then every time someone clicks on one of the buttons write in the log with the parameters and the button that was clicked. Each button has its own function so I'm assuming I would just create the function and then add it at the beginning of each function using the function's parameters.

So I would need to create the txt file using onLoad() I guess, and then create a function to write in the .txt file every time a button is clicked.

I tried:

function WriteToFile(passForm) {

    set fso = CreateObject("Scripting.FileSystemObject");  
    set s = fso.CreateTextFile("logs\log1.txt", True);
    s.writeline("HI");
    s.writeline("Bye");
    s.writeline("-----------------------------");
    s.Close();
 }

but had no luck and I get an error saying Object expected at the beginning of the set fso line.

Any ideas on how to solve this or implement it in a better way?

UPDATE So I just want to create a log file and get filled with data every time a user clicks on a buttons just so I know who is clicking what. All of my files are in a server so I just want to create the text file there and fill it out with information.

9
  • Are you sure we are talking about JavaScript (emphasis on Script) here …? Commented Apr 8, 2013 at 14:02
  • @CBroe well, I have experience with JavaScript and that's why I would like to do it this way. What else do u have in mind? Commented Apr 8, 2013 at 14:03
  • You want to log to a text file on the client's computer? Commented Apr 8, 2013 at 14:06
  • @MikeSmithDev I have everything in my own server, so I want it to create the file where the rest of the files are. Something like logs\logfile.txt Commented Apr 8, 2013 at 14:09
  • Ajax Is the Best Option, and it will work fine Commented Apr 8, 2013 at 14:12

3 Answers 3

3

Say You have following HTML MARKUP with Different Buttons

 <input type='button' name='button1' class='capture' />
 <input type='button' name='button2' class='capture' />
 <input type='button' name='button3' class='capture' />

And when a button is clicked you get name of button and send it to server to write on logfile with following ajax call. Assuming you have PHP on server side

 $(".capture").click(function(){

    var buttnName=$(this).attr('name');
    $.ajax({
      type:"POST",
      data:"ClickedButton="+buttonName, 
      url: "server.php",
      success: function(data){

      alert('Written in Log File');
    }
    }); // END Ajax 
    });

in server.php following code will be used to write on log file

  $myFile = "logfile.txt"; \\Considering the text file in same directory where server.php is
  $fh = fopen($myFile, 'w') or die("can't open file");
  $stringData =$_POST['ClickedButton'] ;
  fwrite($fh, $stringData);
  fclose($fh);

is not simple? Note: You Need Jquery for this purpose

Sign up to request clarification or add additional context in comments.

3 Comments

I have no experience with PHP though :( I just paste that in a file and save it as php?
Then You just have to Change the Server side coding and then you good to go ;)
@MuhammadHaseebKhan I want to ask, can we log something over AWS Cloudwatch with this approach ?
2

JavaScript, in no browser (by default), has access anywhere in the filesystem. Not even a little own space. You might want to look into the HTML5 Storage http://www.html5rocks.com/en/features/storage.

If a user would request for a file.txt with a log-report, you could generate a download call and fill the content returned with what is stored in the Storage object.

9 Comments

I don't want the user to know about this existing log, this is for developing purposes only to see which buttons are being used the most
Then you totally missed the subject of JavaScript's use. Everything on JavaScript's side (client-side) is visible for anyone. You will want to store that data on the server and retrieve it with XHR calls as your closest solution then. What server-side script are you working with?
@randomizertech if they are clicking a button, then you likely have some sort of postback... you'd do your logging on the server during that postback.
@Allendar maybe I did but I feel like this is going in the wrong direction. I just want to create a text file, hopefully using JS, and then write on the file every time a button is clicked (or a function called) it shouldn't be that complicated.
It can't be done, unless you tell your users to enable FileSystem-access first. That would never work sadly.
|
1

Are you trying to do this?

http://www.yaldex.com/wjscript/jsfilecreateTextFile.htm

If so, that uses an active x object. That would be an ie thing.

5 Comments

That's fine, I am using IE anyways
It gives me a warning about ActiveX, is there any way to not show that? Also, it gives me an error saying Bad file name or number
Are all your "users" also using Internet Explorer? Localized stuff is not advised. Especially the ancient ActiveX objects ;)
@Allendar yeah, the default browser used is IE8
This is exactly what I am looking for but I am getting an error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.