Javascript 使用jquery跟踪点击-php跟踪

Javascript 使用jquery跟踪点击-php跟踪,javascript,php,jquery,hyperlink,Javascript,Php,Jquery,Hyperlink,我试图跟踪特定链接的点击次数。但首先,我需要测试我的脚本的所有链接,而不管id如何 我在头部标签之间有这个: <script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js'></script> 在身体标签之间的某个地方,我有: <script type="text/javascript"> $(document).r

我试图跟踪特定链接的点击次数。但首先,我需要测试我的脚本的所有链接,而不管id如何

我在头部标签之间有这个:

<script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js'></script>
在身体标签之间的某个地方,我有:

    <script type="text/javascript">
   $(document).ready(function() {

   $('a').click(function(e) {

   // ++++ Not sure what this condition does ++++
   if($(this).attr("href").indexOf("http")==1) {

      //prevent the redirect;
      e.preventDefault();

      //do your tracking 
      $.ajax({
          url: 'tracking.php',
          data: "link=" + $(this).attr("href"),
          complete: function(){
              //now do the redirect
              window.location = $(this).attr("href");
          } 

     }); 
  } 

   }); 


   }); 
   </script>
为了进行测试,tracking.php如下所示:

<?php
$tckfile = fopen("tracking.txt", "w") or die("Unable to open file!");
$txt = "Test" .  "\r\n";
fwrite($tckfile, $txt);
fclose($tckfile);
?>
我做了更改并重新加载了我的页面,但似乎不起作用。我在stackoverflow上发现了这段代码,不确定它是否有效

有什么想法吗

谢谢

删除您的indexOfhttp,否则没有http的链接将无法计数

<script type="text/javascript">
   $(document).ready(function() {

   $('a').click(function(e) {
   if($(this).attr("href")){
      //prevent the redirect;
      e.preventDefault();

      //do your tracking 
      $.ajax({
          url: 'tracking.php',
          data: "link=" + $(this).attr("href"),
          complete: function(){
              //now do the redirect
              window.location = $(this).attr("href");
          } 

     }); 
  } 

   }); 


   }); 
   </script>

你能告诉我你在哪里找到的吗?该条件检查链接的href属性中是否包含文本http。此外,如果有任何错误,请向我们显示。请尝试警报$this.attrref.indexOfhttp;请注意,如果{}块是您不理解的。请注意,如果您的锚标记看起来像这样,那么您的代码将失败。我在这里找到它:。我没有看到错误,但我的php似乎没有执行。好的,我会试试警报。我的链接是一个带有http的完整链接,因此它应该可以工作。@monkeyzeus这很有趣,警报显示0 0,带有一个如下所示的链接标记:>我的链接01-我不知道为什么,因为它包含http,警报显示-1,带有一个如下所示的链接标记:,这是因为它没有http,对吗?谢谢各位