javascript onclick 写入.txt 文件

发布于 2025-01-04 16:41:35 字数 805 浏览 1 评论 0原文

我有一些 swf 应用程序,当在页面上单击 swf 文件的链接时,这些应用程序会在灯箱中打开,

这纯粹是触发灯箱所需的标准链接

<a href"http:// {url} /swf/ {file} .swf">Name</a>

我一直在尝试实现一个脚本来写入时间戳(日期('U' )在 php 编码中,因为这是我需要时间戳的格式)和单击到 txt 文件的链接的“href”值...我一直在使用 onClick="function()"

但我似乎无法得到它去工作......

我希望将行写入这样的 txt 文件中,

1328984654|http:// {url} /swf/ {file} .swf
1328984829|http:// {url} /swf/ {file2} .swf

我假设换行符是用 \r\n 实现的,

我不一定需要写入文件扩展名,但是我认为编写完整的链接 url 会更容易。 ..

我一直使用的方法是提交onclick的隐藏表单,但它不是很经济,因为我需要隐藏的iframe等等......太多的工作,当我认为这可以在一个脚本中实现时...... 。

我可以轻松更改现有的链接生成脚本,所以 它可以添加

onClick="new_function()"

或任何变体,以便能够将其链接到该函数....

我对 javascript 不太有经验,而且我知道点击脚本需要在客户端:(

如果有人可以的话,请提前致谢帮助我,干杯

i have some swf apps that open in a lightbox when a link to an swf file is clicked on the page

it is purely standard link required to trigger the lightbox

<a href"http:// {url} /swf/ {file} .swf">Name</a>

i have been trying to implement a script to write a timestamp (date('U') in php coding, as that is the format i need the timestamp) and the "href" value of the link clicked to a txt file ... i have been using onClick="function()"

but i cannot seem to get it to work....

i am hoping to have lines written to the txt file like this

1328984654|http:// {url} /swf/ {file} .swf
1328984829|http:// {url} /swf/ {file2} .swf

i would presume the line breaks are implemented with \r\n

i dont necessarily need the file extention to be written, however i would presume it would be easier just to write the full link url....

the method i have been using was hidden form that submits onclick but its not very economical, as i need hidden iframe blah blah ...... too much work, when i think this could be implemented in one script....

i can easily change my existing link generating script, so that it can add

onClick="new_function()"

or any variation of this to be able to link it to the function....

i am not very experienced with javascript, and i know the click script will need to be client side :(

thanks in advance if anyone can help me, cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东走西顾 2025-01-11 16:41:35

JavaScript 无法访问服务器文件系统,也无法向文件写入任何内容。但是,您可以使用 javascript(读取 Ajax)来运行 PHP 文件,该文件将您发送到文件的任何数据写入其中。

对于 Ajax 请求,使用 jQuery 是迄今为止最简单的解决方案,因为在 vanilla JS 中编写 XMLHttpRequests 有点复杂。在 jQuery 中,您可以执行以下操作:

HTML:

<a href"http:// {url} /swf/ {file} .swf" id="mylink">Name</a>

-

$("#mylink").click(function() {
    $.ajax({ 
       type: 'POST', 
       data: {swf : '1328984654|http: {url} /swf/ {file} .swf'}, 
       url: 'write_to_file.php',
       success: function(data) {
          alert('file saved');
       },
       error: function() {
          alert('error');
       }
    });
});

在 PHP 中,您将从 $_POST 超全局中获取数据,如下所示:

$data = $_POST['swf'];

然后使用 file_put_contents 或许多其他解决方案之一将数据保存到文件中,因为您已经让它与表格一起工作我不会进入那部分。您还可以在 PHP 中生成时间戳并同时附加它,无需在 JS 中执行此操作。当然,它比这更复杂一些,例如,您应该对 $_POST 进行正常的 isset 检查,并清理来自客户端的任何数据,但是如果您自己进行一些阅读和测试,您会发现很快就可以完成,这实际上并不难,只需几行代码就足以获得您要尝试执行和运行的基本示例。

Javascript does not have access to the server file system and can not write anything to a file. However you can use javascript (read Ajax) to run a PHP file that writes whatever data you send it to a file.

For Ajax requests using jQuery is by far the easiest solution, as writing XMLHttpRequests in vanilla JS is a bit more complicated. In jQuery you would do something like:

HTML:

<a href"http:// {url} /swf/ {file} .swf" id="mylink">Name</a>

-

$("#mylink").click(function() {
    $.ajax({ 
       type: 'POST', 
       data: {swf : '1328984654|http: {url} /swf/ {file} .swf'}, 
       url: 'write_to_file.php',
       success: function(data) {
          alert('file saved');
       },
       error: function() {
          alert('error');
       }
    });
});

In PHP you'll get the data from the $_POST superglobal like so:

$data = $_POST['swf'];

Then use file_put_contents or one of the many other solutions for saving the data to your file, and since you've had this working with a form I'm not going to go into that part. You could also generate the timestamp in PHP and append that at the same time, no need to do that in JS. It is of course a bit more involved than this, for instance you should do the normal isset check on $_POST, and sanitize any data that comes from the clientside, but if you do a little reading and testing on your own you'll figure it out in no time, it's really not that hard, and just a few lines of code is enough to get a basic example of what you are trying to do up and running.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文