3

I'm new to javascript and the following code isn't working:

<script>
function sendMail()
{
    var yourMessage = document.getElementById("message").value
    var subject = document.getElementById("selectList").value
    var mail="mailto:chrisgreg23@googlemail.com?subject="+subject+"&body="+yourMessage;

    window = window.open(mail, 'emailWindow')
}
</script>

I just want a mail client window to open with the subject and body already done.

Help?

EDIT:

I've also tried this:

<script>
function sendMail()
{
    var yourMessage = document.getElementById("message").value
    var subject = document.getElementById("selectList").value
    var mail="mailto:chrisgreg23@googlemail.com?subject="+subject+"&body="+yourMessage;

    $(this).attr('href', mail);
}
</script>

Ive got that now, still not working.

Chris G
  • 449
  • 1
  • 5
  • 19

1 Answers1

11

Your code should look like this instead:

<script>
function sendMail()
{
    var yourMessage = document.getElementById("message").value;
    var subject = document.getElementById("selectList").value;
    document.location.href = "mailto:chrisgreg23@googlemail.com?subject="
        + encodeURIComponent(subject)
        + "&body=" + encodeURIComponent(yourMessage);
}
</script>
Doug S
  • 10,146
  • 3
  • 40
  • 45
  • 1
    This helped me so much. Thank you very much – Singh Aug 17 '15 at 15:52
  • 1
    This doesn't appear to work on the latest version of chrome. Setting location.href, using window.open() or using jquery attr('href') with a mailto: link does nothing. – PeterT Feb 25 '17 at 20:37