Html 使用 Javascript 创建日志文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15881108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:21:11  来源:igfitidea点击:

Create log text file using Javascript

javascripthtmlloggingtexttext-files

提问by randomizertech

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

我一直在尝试这样做,但没有运气,所以我决定问 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.

我想创建一个 .txt 文件作为日志,然后每次有人点击其中一个按钮时,都会在日志中写入参数和点击的按钮。每个按钮都有自己的功能,所以我假设我只是创建该功能,然后使用该功能的参数将其添加到每个功能的开头。

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.

所以我想我需要使用 onLoad() 创建 txt 文件,然后创建一个函数来在每次单击按钮时写入 .txt 文件。

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 fsoline.

但没有运气,我收到一条错误消息,说在行的开头期望对象set fso

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

关于如何解决这个问题或以更好的方式实施它的任何想法?

UPDATESo 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.

更新所以我只想创建一个日志文件并在每次用户单击按钮时填充数据,以便我知道谁在单击什么。我的所有文件都在服务器中,所以我只想在那里创建文本文件并填写信息。

回答by Muhammad Haseeb Khan

Say You have following HTML MARKUP with Different Buttons

假设您遵循带有不同按钮的 HTML MARKUP

 <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

当一个按钮被点击时,你会得到按钮的名称并将其发送到服务器以使用以下 ajax 调用写入日志文件。假设您在服务器端有 PHP

 $(".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.phpfollowing code will be used to write on log file

server.php下面的代码将被用来写在日志文件

  $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

是不是很简单? 注意:为此您需要 Jquery

回答by Muhammad Haseeb Khan

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.

JavaScript,在没有浏览器的情况下(默认情况下),可以访问文件系统中的任何地方。甚至没有一点自己的空间。您可能需要查看 HTML5 存储http://www.html5rocks.com/en/features/storage

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

如果用户请求file.txt使用日志报告,您可以生成下载调用并使用存储在 Storage 对象中的内容填充返回的内容。

回答by John Green

Are you trying to do this?

你想这样做吗?

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

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

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

如果是这样,则使用活动的 x 对象。那将是一件事情。