Click Logging with JavaScript

后端 未结 5 594
挽巷
挽巷 2021-01-03 01:55

I want to log all clicks on a link.

I\'ve written a little logger, which can be called by an url (returns an empty page). This url is called with a jquery-ajax-metho

5条回答
  •  猫巷女王i
    2021-01-03 02:53

    I think the reason FF is giving you poor results is because you're leaving the page before the action takes time to execute. As mhartman's link mentions if you use a target on your external link it should work fine. If you can't do that then you may have to wait for the log to complete, although you may see delays in navigation.

    HTML code

    Click
    

    the

    function loggClick(e) {
      if (!e) e = window.event;
    
      e.preventDefault();  // cancels the link
    
      var theLink = this.href;  // stores the link for later
    
      $.ajax({
         async: false,
         type: "POST",
            url: "Logger.ff", //dynamic url to logging action
            data: {
                sid: 'abc123' //random data
            },
            contentType: "application/x-www-form-urlencoded; charset=UTF-8",
            cache: false,
             complete: function() {
               // navigate when the log completes
               this.location.href = theLink;
             }
        });
        return true;
      }
    }
    

提交回复
热议问题