如何使用javascript创建电子邮件按钮 [英] How to create an email button with javascript

查看:97
本文介绍了如何使用javascript创建电子邮件按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到一个真正符合我的问题的文章,所以这里我们去:

I couldn't find a post that really matches my question, so here we go:

我想通过我的网站实现通过邮件分享按钮,所以当你点击按钮,我们来说outlook或thunderbird打开,并给你选择在一个新的邮件中共享网站链接。

I want to implement a "share via mail" button to my website, so when you click the button let's say outlook or thunderbird opens and gives you the option to share the website link within a new mail.

我不太确定,我想我将无法完全用html来完成,因为当你链接到他们的共享站点时,我们也会运行JS。

I'm not quite sure but I think I won't be able to do it completely with html only, cause facebook i.e. is also running JSs when you link to their share-site.

推荐答案

您不需要javascript。只是一个简单的 HTML

You don't need javascript for this. Just a simple HTML:

<a id="emailMe" href="mailto:example@website.com">e-mail me</a>

您还可以定义主题,但您必须记住,您只能使用字符az和数字0-9。其他字符必须是网址编码的,例如主题这是一个主题应该像这样编码

You can also define a subject but you must remember that you can only use characters a-z and numbers 0-9. Other characters must be url-encoded, e.g. subject "This is a subject" should be encoded like this

<a id="emailMe" href="mailto:example@website.com?subject=This%20is%20a%20subject">e-mail me</a>

如果你不想手动对每个字符进行编码,这是很明显的,

If you don't want to manually encode each character which is pretty obvious, here comes javascript finally:

var subject = "This is a subject";
var subjectEncoded = encodeURIComponent(subject);
document.getElementById('emailMe').href = "mailto:example@website.com?subject=" + subjectEncoded;

这篇关于如何使用javascript创建电子邮件按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆