How to prevent mailto event from opening a new tab in browser

北城以北 提交于 2019-12-03 01:12:23

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

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

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.

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);
ezchx

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.

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

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>
Laarni Cortez
<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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!