问题
I have a web application from which the user can send an email using a mailto link. Basically, I concatenate a string that contains the subject, body and recipients and then the code triggers a mailto, somewhat like this:
var MailToLink = 'mailto:' + TheEmailString;
window.open(MailToLink, '_blank'); //option 1
window.open(MailToLink); //option 2
Initially, I only had option 2 because on my dev machine I have Outlook. But as I started some alpha testing, another user was using Gmail and the mailto link would open in the same window as my app so I changed my code to option 2 so that the email opens in another window. The problem now is that when the user is on Outlook, the link opens both an Outlook message and a new blank window.
Is there a way to detect if the user is using a web-based email client or a local client? I would like to do something like this:
if (IsUsingWebMailClient) {
window.open(MailToLink, '_blank');
} else {
window.open(MailToLink);
}
I'm not sure if this is even possible; how do I detect the mailto client using JavaScript?
回答1:
It's a bit of a hack, but that works. The time out may vary, you can test with more and less milliseconds.
<html>
<head>
<script type="text/javascript">
function mailTo(address){
var windowRef = window.open('mailto:'+address, '_blank');
var body = windowRef.document.getElementsByTagName('body')
if(!body.lenth) setTimeout(windowRef.close,300)
}
</script>
</head>
<body>
<input type="button" value="send email" onclick="mailTo('email@gmail.com')" >
</body>
</html>
来源:https://stackoverflow.com/questions/31837370/detecting-web-based-mail-client-vs-local-mail-client-for-a-mailto-link