javascript在单击链接时更改iframe位置

javascript在单击链接时更改iframe位置,javascript,html,iframe,Javascript,Html,Iframe,我在同一页上有很多链接。这些链接将启动不同的下载。 我想做的是,当单击链接开始下载时,我还想将iframe位置更改为php页面,以增加下载命中计数器 代码需要在每个链接上的onClick事件中,因为我在同一页上有这么多 我想要这样的东西: <a href="download001.zip" onClick="iframe.location.href='hits_counter.php?file=download001>Start download 001</a><b

我在同一页上有很多链接。这些链接将启动不同的下载。 我想做的是,当单击链接开始下载时,我还想将iframe位置更改为php页面,以增加下载命中计数器

代码需要在每个链接上的
onClick
事件中,因为我在同一页上有这么多

我想要这样的东西:

<a href="download001.zip" onClick="iframe.location.href='hits_counter.php?file=download001>Start download 001</a><br>
<a href="download002.zip" onClick="iframe.location.href='hits_counter.php?file=download002>Start download 003</a><br>
<a href="download003.zip" onClick="iframe.location.href='hits_counter.php?file=download003>Start download 003</a><br>
...



...

这两个事件都必须激活:下载文件并更改iframe href

如果有很多,onClick不是解决方案

使用jquery,您可以通过以下方式处理所有问题:

$("a").click(function(){
    $.get("hits_counter.php",{ file:$(this).attr("href") });
});

而且不再需要iframe

您可以从服务器端提供文件并增加计数器。发送文件供下载的代码如下所示。因此,您的hits_counter.php可以是这样的



Source-

为什么不让PHP脚本既有价值又能提供内容呢?我真的需要使用iframe,因为命中计数器位于不同的服务器上,我无法让跨域提交工作。。。。“file=”(get)的值也不同于href。。。。我只是用它来简化我的示例问题是我在托管文件的服务器上没有mysql。命中计数器需要在另一台服务器上,这就是为什么我考虑将查询发送到iframe
$file = $_GET['filename'];

if (file_exists($file)) {
  header('Content-Description: File Transfer');
  header('Content-Type: application/octet-stream');
  header('Content-Disposition: attachment; filename='.basename($file));
  header('Content-Transfer-Encoding: binary');
  header('Expires: 0');
  header('Cache-Control: must-revalidate');
  header('Pragma: public');
  header('Content-Length: ' . filesize($file));
  ob_clean();
  flush();
  readfile($file);

  //increment counter based on file name
  exit;
}