javascript mailto not working in chrome mobile bro

2019-08-09 08:19发布

问题:

mailto via javascript not working in mobile chrome browser

window.location.href = "mailto:linto.cet@gmail.com?subject=subject&body=body"

is not working in mobile google chrome browser

actual source

回答1:

Chrome on Android is blocking the redirects to apps that are not made via a user gesture.

So via javascript it's not possible to redirect the user to the mail app since Chrome 40, only if you put it for example in a button href, that will work when user clicks the button.

You can read more in chromium forum

If you inspect the Chrome console you will a warning, something like: Navigation is blocked: mailto:?...



回答2:

I am posting an answer as this is possible.

Create a hidden from view / temporary link element and simulate the click.

var linkElement = document.createElement('a');
linkElement.style.visibility = 'hidden';
linkElement.style.position = 'absolute';
linkElement.href = 'mailto:linto.cet@gmail.com?subject=subject&body=body';
document.body.appendChild(linkElement);

and later when you want to trigger and open the mail client:

linkElement.click();