Mailto using Javascript? [duplicate]

♀尐吖头ヾ 提交于 2020-01-23 03:33:25

问题


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.


回答1:


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>


来源:https://stackoverflow.com/questions/21028939/mailto-using-javascript

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