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条回答
Deceive 欺骗
2楼-- · 2020-05-24 20:54
<a className="prom-links-anchor" tabIndex="0" aria-label="Email" href={ "mailto:" + eid + "@yahoo.com"}   
   role="link" nocm="true" title={ "Email"} rel="email" data-cs={contentSource} data={resultPosition + 1} 
   onMouseDown={this.handleMouseDown.bind(this)} onKeyDown={this.handleKeyPressEvent.bind(this)} 
   onContextMenu={e=> e.preventDefault()}
  >
  <div className="icon-email" title="Email" href={"mailto:" + eid + "@yahoo.com"} rel="email" 
       data-cs={contentSource} data={resultPosition + 1} />
  <div className="icon-label-email" aria-hidden="true" href={"mailto:" + eid + "@yahoo.com"} 
       title={"Email"} rel="hc-email" data-cs={contentSource} data={resultPosition + 1}
    >Email</div>
</a>
查看更多
Deceive 欺骗
3楼-- · 2020-05-24 20:55

For the record:

create a anchor tag with the target attribute like this:

<div>
    <a target="_self" href="mailto:mail1@domain1.com;%20mail2@domain2.com?subject=Mail%20sending&body=etc...">
        Send Mail
    </a>
</div>
查看更多
Ridiculous、
4楼-- · 2020-05-24 20:56

The blank tab is opened by window.open(). You don't need that.

The syntax for a mailto link should be something like

<a href="mailto:your@email.address?subject=Comments about the color blue">Contact Us</a>

See http://www.addressmunger.com/mailto_syntax_tutorial/ for more details.

查看更多
混吃等死
5楼-- · 2020-05-24 21:01

No, that strictly depends on how your browser handles new tabs. I've spent hours looking for a work around, solution, anything...

firefox: options -> tabs

safari: preferences -> tabs

查看更多
做自己的国王
6楼-- · 2020-05-24 21:05

Thank you for the edit. There is indeed an alternative:

window.location.href = "mailto:mail@domain.tld";
alert("Thank you!");

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

I did not really get this one. You are not leaving the website when using mailto: with window.location.href

查看更多
迷人小祖宗
7楼-- · 2020-05-24 21:05

Just close the window after a short interval:

var mailto_link = 'mailto:'+email+'?subject='+subject+'&body='+body_message;
var win = window.open(mailto_link,'emailWindow');
setTimeout(function() { win.close() }, 500);
查看更多
登录 后发表回答