Search code examples
javascripthtmlajaxanchormailto

Checking If the User clicked on Mail Link


I have a page with an email link

<a href="mailto:email@address.com?subject=Test Story&amp;body=abc.com
">Email Link</a>

I want to check that If User clicks on this link open the default Email client, but I also want to update in Database that User has mailed. I know this is all happening at client side, Is there any way I can check either User clicked on it or not? onclick is not there for anchor tag. Any help in this regard.


Solution

  • You need to listen for the click event on <a> tags with a "mailto:" link. Assuming you're using jQuery, here is the code:

    $('a[href^="mailto:"]').on('click', function(e){
      var email = $(this).attr('href').replace('mailto:', '');
      // submit action to server here.
    });
    

    Update: make sure this code is running after the DOM ready event, like so:

    jQuery(function($){
      $('a[href^="mailto:"]').on('click', function(e){
        var email = $(this).attr('href').replace('mailto:', '');
        // submit action to server here.
      });
    });