1

In my jQuery Mobile App, I have a mailto link, its href attribute is dynamically generated and it is 'clicked' via jQuery.Here is the link code:

<a id="mealLink" href="mailto:123@123.com" style="display: none;">This is the mailto 
link</a>

A click handler is attached to it like this:

$('#mailLink').bind('click', function() {
window.location.href = $(this).attr('href'); 
});

Lastly,a function creates the href attribute for the link with emailaddress, subject and message body and click is simulated via jQuery:

$emailAddress= ..

$subject= ....
$body=...
$emailString="mailto:"+$emailAddress+$subject+$body;
$emailLink= $("#mealMail");
$emailLink.attr("href",$emailString);
$emailLink.click();

Now, this code is working perfectly in: Mozilla desktop Safari desktop Android

But not working in: Safari Mobile Chrome desktop

Any suggestions?

user1107888
  • 1,481
  • 5
  • 34
  • 55

2 Answers2

1

After searching for complex solutions, I found a much easier solution by chance. The issue here is that if a mailto link is directly clicked, it works in all browsers, but if it is indirectly clicked, such as via jQuery .click() function, it does not work in all browsers. Therefore, here is my implementation:

<a href='#mailtolink' id="emailLink">This is a mail to link</a>
$emailAddress= ..

$subject= ....
$body=...
$emailString="mailto:"+$emailAddress+$subject+$body;
$emailLink= $("#emailLink");
$emailLink.attr("href",$emailString);

Now, depending upon the context of an application, the href parameter of the link can be setup and when this link is clicked, it works. I have tested in following browers:

  1. Mozilla Firefox Desktop
  2. Safari Desktop
  3. Chrome Desktop
  4. Safari mobile on ipad 1
user1107888
  • 1,481
  • 5
  • 34
  • 55
  • keep in mind that if this for a public website it's not good to use mailto. A lot of people don't use any mail client now-days. Still great solution. – Rui Lima Aug 16 '12 at 22:38
  • I had the same issue where I was originally saying `window.location = mailto;`. After updating the href attr of an a tag, it's working perfectly. – Evan Siroky Sep 16 '15 at 20:58
0

You should not use mailto:
Check if is not better to create a simple "Contact us" form. But still, take a look at this: i-cant-get-mailto-links-to-open-the-mail-app-from-mobile-safari-when-using-jqto

Community
  • 1
  • 1
Rui Lima
  • 7,185
  • 4
  • 31
  • 42
  • This solution still does not work in Chrome desktop and Safari mobile and additionally breaks Safari desktop as well. – user1107888 Jul 26 '12 at 09:24
  • Sad to here that :( But can't you just create a simple contact us form? as far as user acceptance is much better then the MAIL TO. The last one, opens the Outlook or similar, but a lot of people don't use any of this now days. – Rui Lima Aug 03 '12 at 16:34