47

I currently have a form set up with 5 radio options. I have a switch statement depending on the option you pick and that determines where the email is going to go.

Inside my switch, I have this piece of code.

window.open("mailto:"+emailTo+'?cc='+emailCC+'&subject='+emailSub+'&body='+emailBody);

It all works fine when it opens up my email client with all of the content however it also opens a blank page in the browser.

Is there another way to achieve this or prevent a blank window from opening but still make it as if you clicked on the href:mailto ?

SBB
  • 8,560
  • 30
  • 108
  • 223
  • I think this is a browser setting, therefore you have no control over how this works. – Liam Jan 30 '14 at 16:14
  • You may use `window.location.href = "mailto:mail@example.org";` but this will work only if in browser setting Gmail opening is not setted, etc. – antyrat Jan 30 '14 at 16:15
  • You can just close that window, can't you? – Ale Jan 30 '14 at 16:17
  • 3
    possible duplicate of [How to prevent mailto event from opening a new tab in browser](http://stackoverflow.com/questions/13457684/how-to-prevent-mailto-event-from-opening-a-new-tab-in-browser) – Jonathan Lonowski Jan 30 '14 at 16:27

4 Answers4

57

Instead of:

window.open("mailto:"+emailTo+'?cc='+emailCC+'&subject='+emailSub+'&body='+emailBody);

You can try:

location.href = "mailto:"+emailTo+'?cc='+emailCC+'&subject='+emailSub+'&body='+emailBody;
Rodrigo5244
  • 5,145
  • 2
  • 25
  • 36
32

The second argument in window.open is the target.

window.open('mailto:'+emailTo+'?cc='+emailCC+'&subject='+emailSub+'&body='+emailBody, '_self'); should do the trick.

Jeremy
  • 2,870
  • 3
  • 23
  • 31
BigBadBigBad
  • 421
  • 5
  • 9
6

Location.href doesn't seem to work in chrome. I do it like this:

x=window.open("mailstring");
x.close();

Works perfect for me.

slfan
  • 8,950
  • 115
  • 65
  • 78
-4

After your "window.open" statement, try running an "if statement" to check and see if a new window was opened so that it will close.

if (win && win.open && !win.closed) 
    {   
    win.close();
    }

This will happen very fast so the users might notice that a window opened and closed before the email application opened.

Spr89
  • 81
  • 1
  • 9