Mailto link 将当前域名添加到主题行

Mailto link that adds current domain name to subject line

我正在尝试创建一个 mailto link,在单击时以编程方式在邮件主题中包含当前域。

这是我目前拥有的:

<div class="cta">
Make an enquiry at <a href="javascript:'mailto:sales@hashimaziz.com?subject=Sales%20Inquiry%20RE:%20' + window.location.hostname;">sales@hashimaziz.com</a>
</div>

JavaScript 本身似乎可以工作,但是当单击 mailto link 时,它会转到一个打印 mailto 命令本身文本的白页。这可以在网站本身上进行测试 here.

mailto link 有什么问题阻止它正常运行?

JavaScript URL,当导航到时,评估表达式并打印它。

您可以使用:

<div class="cta">
Make an enquiry at <a href="javascript:void(window.location.href%3D'mailto%3Asales%40hashimaziz.com%3Fsubject%3DSales%2520Inquiry%2520RE%3A%2520'%2Bwindow.location.hostname)">sales@hashimaziz.com</a>
</div>

或者,可能更好:

<div class="cta">
    Make an enquiry at <a id="contact-link">sales@hashimaziz.com</a>
</div>

<!-- New script tag not necessary. -->
<script>
    document.getElementById("contact-link").href = 'mailto:sales@hashimaziz.com?subject=Sales%20Inquiry%20RE:%20' + window.location.hostname;
</script>