How to prevent mailto event from opening a new tab

2020-05-24 20:27发布

I am using a mailto: filled in JavaScript to send information throughout my web application, but everytime a user presses the Send button, it opens a new tab in the browser before opening the mailing application (Outlook, Gmail, etc).

Is there any way to prevent the blank tab from opening?


Edit: This issue is encountered in all following major browsers : Internet Explorer, Firefox and Google Chrome.

I am using window.open() to send emails, is there any known alternatives?

Here is how I send the email:

var mailto_link = 'mailto:'+email+'?subject='+subject+'&body='+body_message;
var win = window.open(mailto_link,'emailWindow');

I don't want to use window.location.href since I am displaying a message after the user sent the email.

8条回答
趁早两清
2楼-- · 2020-05-24 21:09

Try naming the window (myWindow) and adding a close() command:

<script>
    myWindow=window.open("mailto:emailaddress@example.com");
    myWindow.close();
</script>';

This should close the extra browser window and keep the email application open. At least it worked for me.

查看更多
We Are One
3楼-- · 2020-05-24 21:13

The window.location.href solution by AmShaegar works pretty well but it caused side effect in a complex application I have been developping.

I finally came up with this solution one might be interested in:

$('<iframe src="mailto:mail@domain.tld">').appendTo('body').css("display", "none");

See this plunker: http://plnkr.co/edit/J0LvQU?p=preview

查看更多
登录 后发表回答