A small javascript file to find email addresses in web pages and turn them into clickable <a href=mailto.../> links.

It is common to avoid using <a href=mailto...> in web pages to prevent spammers from harvesting your email address. If you put your email address in plain text on the page and add markup to identify it, the javascript file will add additional <a> markup to turn it into a normal “clickable” anchor element.

It is just one file which you’ll find in the root directory of the create-mailto repo. Or load the raw version content of create-mailto.js in your browser and save it.

Copy the javascript file to your webserver and add it to your page with a script element:

<script src="create-mailto.js">

Then, in the web page, add markup to your email addresses:

<span class="email"></span>

plain html html with create-mailto.js
<span class="email"></span> <span class="email"><a href=""></a></span>

That’s it!

reusing existing classnames

If you use microformats or microdata, create-mailto will convert email addresses without any additional markup:

markup type plain html html with create-mailto.js
microformats hCard 1.0 <span class="email"></span> <span class="email"><a href=""></a></span>
microformats2 hcard <span class="p-email"></span> <span class="p-email"><a href=""></a></span> microdata <span itemprop="email"></itemprop> <span itemprop="email"><a href=""></a></span>

using data- attribute

You can also use this script in situations where the email address does not appear in the web page, by using data-* attributes. Just add a data-email to your element:

plain html html with create-mailto.js
<span data-email="">email me</span> <span data-email=""><a href="">email me</a></span>
<img src="foo.jpg" alt="email me" data-email=""> <a href=""><img src="foo.jpg" alt="email me" data-email=""></a>

automatically excluded elements

create-mailto will not change any part of a document’s <head> element. There are also restrictions on interactive and form elements. For example, it will not add <a> markup inside a <label> element, since clicking a <label> element changes focus to the associated form input. It should not have additional effects, e.g., opening a compose email window, because such behavior would likely be unexpected. In addition, create-mailto will ignore void elements like <col> and <br>. The list below is not exhaustive:

plain html html with create-mailto.js (no markup changes)
<label class="email"><label> <label class="email"><label>
<label><span class="email"></span><label> <label><span class="email"></span><label>
<option class="email"><option> <option class="email"><option>
<textarea class="email"></textarea> <textarea class="email"></textarea>
<details class="email"><details> <details class="email"><details>
<audio data-email="" controls src="foo.ogg"></audio> <audio data-email="" controls src="foo.ogg"></audio>
<button class="email"><button> <button class="email"><button>

tell create-mailto.js to ignore elements

If you are using a create-mailto class for some other reason e.g., you use <span class="email">username@example</span> in your markup, but you don’t want that instance turned into a link, you can add ignore-create-mailto to its class attribute. This will work on an element and any of its children.

plain html html with create-mailto.js (no markup changes)
<span class="email exclude-create-mailto"></span> <span class="email exclude-create-mailto"></span>
<p class="exclude-create-mailto"><span class="email"></span></p> <p class="exclude-create-mailto"><span class="email"></span></p>

legacy broswer support

The script create-mailto.js uses modern javascript features. It should work with recent releases of all major browsers. If you need to support legacy browsers (e.g. Internet Explorer), then try the jQuery version create-mailto.jquery.js. You will need to include jQuery in your web page.


There are test web pages to see if create-mailto.js is correctly inserting mailto: links where it should and not creating them where it shouldn’t.