问题
I have a mailto link on my site that is working fine. However, I want to perform a .click() event on it to log when the user clicks that link. I have the .click event working and performing the ajax request, but now the mailto link doesn't open the email client. Is it possible to run a jquery function before the client opens, but still make the client open?
here is the code I have (it just opens a blank window in the browser)
<script type='text/javascript'>
jQuery('span.email a').on('click',function (){
jQuery.ajax({
type: 'POST',
url: '', //url here
data: {comment: 'Email Sent', category : 'EE', store_id: '" . $this->store_id . "'},
success: function (data){jQuery('#alerts').html(data);}
});
window.location.href= $(this).prop('href');
});
</script>
回答1:
You don't need the window.location.href= $(this).prop('href');
. If you just let the click continue on as normal, it should do what you want.
回答2:
In order for this to work you will need to set the async to false.
See http://jsfiddle.net/5nwu7/3/
<script type='text/javascript'>
jQuery('span.email a').on('click',function (){
jQuery.ajax({
type: 'POST',
url: '', //url here
data: {comment: 'Email Sent', category : 'EE', store_id: '" . $this->store_id . "'},
success: function (data){jQuery('#alerts').html(data);},
async: false
});
//You don't need this ---window.location.href= $(this).prop('href');
});
</script>
This is because some browsers will cancel your current ajax request when opening the mail client. You can test this by going to that jsfiddle I posted and removing the async line (tested in chrome 23.0.1271.97 m, windows 7, outlook 2007.)
来源:https://stackoverflow.com/questions/14164966/mailto-link-with-jquery