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

47,112

Solution 1

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

window.location.href = "mailto:[email protected]";
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

Solution 2

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:[email protected]">').appendTo('body').css("display", "none");

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

Solution 3

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:[email protected]?subject=Comments about the color blue">Contact Us</a>

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

Solution 4

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

Solution 5

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

<script>
    myWindow=window.open("mailto:[email protected]");
    myWindow.close();
</script>';

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

Share:
47,112
Jeff Noel
Author by

Jeff Noel

Riding my motorcycle is my passion. When I don't ride, I code. My favorite languages are JavaScript, HTML and CSS**. I also know the following languages: PHP, MySQL, MSSQL, C++, C#, Visual Basic, Visual .Net. I wish I knew more in: NodeJS, MVC5 .NET (C#), MEAN stack, AngularJS. Frameworks and libraries I use/know: Wordpress, Moodle LMS, Umbraco, jQuery and its many plugins, jQuery UI, GruntJS, Bootstrap, MVC5.

Updated on July 15, 2022

Comments

  • Jeff Noel
    Jeff Noel almost 2 years

    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.