Javascript location.href to mailto triggers a GET HTTP that gets canceled in Chrome
Asked Answered
P

1

5

I have a button that triggers the following javascript function:

function sendEmail() {
    var mail = 'mailto:[email protected]';
    location.href = mail;
};

In Chrome, this function triggers an HTTP GET to 'mailto:[email protected]', but the HTTP GET has a 'canceled' status in the Inspect Element Network tab, and the email client is not opened.

In IE, the email client also does not get opened.

How can I get the email client to open?

Peroxide answered 18/2, 2013 at 13:17 Comment(0)
C
5

It works for me. But you can try this

function sendEmail() {
    var mail = 'mailto:[email protected]';
    var a = document.createElement('a');
    a.href = mail;
    document.body.appendChild(a); // Add to the DOM
    a.click();
    document.body.removeChild(a); // Remove it back
};
Counterman answered 18/2, 2013 at 13:26 Comment(5)
I tried your function but get the same 'canceled' from Chrome.Arbitrary
Have you configured the default mail client? It also happens when the proper URI handler is not found in the OSCounterman
Yes, it was a problem of the default email client not configured. Thank you very much.Arbitrary
In some situations (until now I can't figure out when and when not) I run in the same problem and Chrome ignores the location.href = "mailto:[email protected]". @Салман: Your solution fixed my problem with Chrome (v39), but unfortunately it doesn't work with Firefox (v35) and IE11. Any thought on how to fix this except of a browser depending switch?Arathorn
@Arathorn See updated code, FF doesn't honor anchor if it hasn't been added to the DOM. So You could add it, simulate the click and remove it back.Counterman

© 2022 - 2024 — McMap. All rights reserved.