JavaScript如何使用Mailto?

3

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

我已经明白了,仍然无法运行。

2
你有收到任何错误吗?话虽如此,使用window.open()可能根本无法完成。在这种情况下,您必须使用链接。 - Pekka
我编辑了我的帖子,但仍然不起作用。 - Chris G
这里可以运行 http://jsfiddle.net/CJTgf/ - Yuriy Galanter
window.location = mail 比起 window.open 更好,因为它不会触发类似 Chrome 的弹出拦截器。 - Niall Paterson
1个回答

11

你的代码应该像这样:

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

1
这对我帮助很大。非常感谢。 - Singh
1
这似乎在最新版本的Chrome上不起作用。 设置location.href,使用window.open()或使用带有mailto:链接的jquery attr('href')什么也不做。 - PeterT

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接