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

html - How to create an email button with javascript - Stack Overflow

programmeradmin2浏览0评论

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

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.

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.

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

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.

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.

Share Improve this question edited Oct 18, 2013 at 19:32 Julius Vainora 48.2k9 gold badges93 silver badges106 bronze badges asked Oct 17, 2013 at 9:03 Andreas SpaethAndreas Spaeth 1831 gold badge4 silver badges13 bronze badges 2
  • developer.mozilla.org/nl/docs/Web-based_protocol_handlers – rene Commented Oct 17, 2013 at 9:05
  • possible duplicate of Automically open default email client and pre-populate content – Vitalii Petrychuk Commented Oct 17, 2013 at 9:05
Add a comment  | 

4 Answers 4

Reset to default 7

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

<a id="emailMe" href="mailto:[email protected]">e-mail me</a>

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:[email protected]?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:[email protected]?subject=" + subjectEncoded;

here you go:

<a href="mailto:[email protected]?subject=just-a-subject">Send a mail</a>

hope it helped.

<a href="mailto:?Subject=SubjectHere&body=ThisIsTheMailtext">

Why would you use javascript? Now it opens and you have to put the recipient in /its empty).

email link with js

var mydiv = document.getElementById("myDiv");
var aTag = document.createElement('a');
aTag.setAttribute('href',"mailto:[email protected]?subject=Subject&body=message%20goes%20here");
aTag.innerHTML = "share via mail";
mydiv.appendChild(aTag);
发布评论

评论列表(0)

  1. 暂无评论