Javascript location.href to mailto launches HTTP GET, which is canceled in Chrome

I have a button that runs the following javascript function:

function sendEmail() { var mail = 'mailto: contact@test.com '; location.href = mail; }; 

In Chrome, this function runs an HTTP GET on mailto: contact@test.com , but the HTTP GET has the status "canceled" on the "Inspect Element Network" tab, and the email client does not open.

In IE, the email client also does not open.

How can I open a mail client?

+5
source share
1 answer

This works for me. But you can try this

 function sendEmail() { var mail = 'mailto: contact@test.com '; 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 }; 
+5
source

Source: https://habr.com/ru/post/973115/


All Articles