Mailto使用Javascript?

时间:2014-01-09 19:04:27

标签: javascript html5 mailto

我是javascript的新手,以下代码无效:

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

我只想要打开邮件客户端窗口,主题和正文已经完成。

帮助?

编辑:

我也试过这个:

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

我现在已经拿到了,仍然没有工作。

1 个答案:

答案 0 :(得分:6)

您的代码应该是这样的:

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