最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you dynamically set a recipient in a mailto link? - Stack Overflow

programmeradmin1浏览0评论

I'm trying to dynamically set the recipient of my mailto link in javascript. I thought that I could just put a javascript variable in the recipient place of the link, but I get errors when trying to do so. Anyone have any suggestions of why this might not be working?

This is what I currently have, which is throwing errors inside my code.


var customerEmail = "[email protected]";

<a href='mailto:' + customerEmail + '?subject=Quote&body=I%20would%20like%20to%20accept%20this%20quote' ><button>Click To Accept</button></a>

I'm trying to dynamically set the recipient of my mailto link in javascript. I thought that I could just put a javascript variable in the recipient place of the link, but I get errors when trying to do so. Anyone have any suggestions of why this might not be working?

This is what I currently have, which is throwing errors inside my code.


var customerEmail = "[email protected]";

<a href='mailto:' + customerEmail + '?subject=Quote&body=I%20would%20like%20to%20accept%20this%20quote' ><button>Click To Accept</button></a>

Share Improve this question asked Oct 4, 2019 at 14:03 MichaelMichael 1,7345 gold badges24 silver badges52 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5

either pull out the concatenation out of the href or use bacticks to fill the href.

<a href={`mailto:${customerEmail}?subject=Quote&body=I%20would%20like%20to%20accept%20this%20quote`} >
   <button>Click To Accept</button>
</a>

You are missing the curly braces inside the href attribute:

var customerEmail = "[email protected]";

<a href={'mailto:' + customerEmail + '?subject=Quote&body=I%20would%20like%20to%20accept%20this%20quote'} >
  <button>Click To Accept</button>
</a>

The curly braces are a special syntax to let the JSX parser know that it needs to interpret the contents in between them as JavaScript instead of a string.

发布评论

评论列表(0)

  1. 暂无评论