问题
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.
回答1:
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
回答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.
回答3:
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.
来源:https://stackoverflow.com/questions/15881108/create-log-text-file-using-javascript