I'm trying to create a mailto link that programmatically includes the current domain in the mail subject when clicked.
This is what I have so far:
<div class="cta">
Make an enquiry at <a href="javascript:'mailto:[email protected]?subject=Sales%20Inquiry%20RE:%20' + window.location.hostname;">[email protected]</a>
</div>
The JavaScript itself seems to work, but when the mailto link is clicked, it goes to a white page that prints the text of the mailto
command itself. This can be tested on the website itself here.
What's wrong with the mailto
link that's preventing it from behaving as it should?
JavaScript URLs, when navigated to, evaluate an expression and print it.
You can use:
<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)">[email protected]</a>
</div>
Or, possibly better:
<div class="cta">
Make an enquiry at <a id="contact-link">[email protected]</a>
</div>
<!-- New script tag not necessary. -->
<script>
document.getElementById("contact-link").href = 'mailto:[email protected]?subject=Sales%20Inquiry%20RE:%20' + window.location.hostname;
</script>