使用 html5 和 javascript 创建带有动态生成正文的电子邮件链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9014470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:24:14  来源:igfitidea点击:

Creating email link with dynamically generated body with html5 & javascript

javascripthtmlemailmailto

提问by vinomarky

Am trying to create a link to create an email to send information to the user, the body of which needs to be filled with data generated by a javascript function, and hope that someone can help.

我正在尝试创建一个链接来创建电子邮件以向用户发送信息,其正文需要填充由 javascript 函数生成的数据,希望有人可以提供帮助。

Idealy, if I could substitute the 'body_blurb' below, with a string returned from a javascript function called at the time of clicking that'd be perfect.

理想情况下,如果我可以用单击时调用的 javascript 函数返回的字符串替换下面的“body_blurb”,那就完美了。

<A HREF="mailto:[email protected]?subject=Data&body=body_blurb">e-mail data</a>

Appreciate your time

珍惜你的时间

回答by mrtsherman

I just assigned an id to the link here, but you could create something more generic if you wanted. Once you have an onclick handler created you can access the url with href. Set this to whatever you want.

我只是为这里的链接分配了一个 id,但如果你愿意,你可以创建更通用的东西。创建 onclick 处理程序后,您可以使用href. 将此设置为您想要的任何内容。

http://jsfiddle.net/f3ZZL/1

http://jsfiddle.net/f3ZZL/1

var link = document.getElementById('email');
link.onclick = function() {
    this.href = "mailto:[email protected]?subject=Data&body=";
    this.href += getBody();
};

function getBody() {
    return 'HelloWorld';
}

回答by talves

Here is the no-spam method

这是无垃圾邮件方法

var link = document.getElementById('email');

link.onclick = function() {
    var name = "you";
    var domain = "yourdomain.com";
    var linker = "mailto:" + name + '@' + domain + "?subject=Data&body=";
    linker += getBody();
    document.location(linker);
};

function getBody() {
    return 'HelloWorld';
}

Fiddle

小提琴

回答by Borealid

You don't actually need to set the hrefvalue. What you're really trying to do is send the browser to a mailto:URL: the hrefis just a means to that end.

您实际上不需要设置该href值。您真正想做的是将浏览器发送到一个mailto:URL:这href只是达到此目的的一种手段。

Leave the hrefblank and make the link have an onClickhandler. In the onClick, feed the browser the mailto:URL (after you build it using your variable). Presto, done.

保留href空白,使链接有一个onClick处理程序。在 中onClick,向浏览器提供mailto:URL(在使用变量构建它之后)。快,完成。