Mailto using Javascript?

28,355

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:[email protected]?subject="
        + encodeURIComponent(subject)
        + "&body=" + encodeURIComponent(yourMessage);
}
</script>
Share:
28,355
Chris G
Author by

Chris G

Updated on August 24, 2022

Comments

  • Chris G
    Chris G over 1 year

    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:[email protected]?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:[email protected]?subject="+subject+"&body="+yourMessage;
    
        $(this).attr('href', mail);
    }
    </script>
    

    Ive got that now, still not working.

  • Singh
    Singh over 8 years
    This helped me so much. Thank you very much
  • PeterT
    PeterT about 7 years
    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.