Skip to content

Instantly share code, notes, and snippets.

@codebryo
Last active August 29, 2015 13:59
Show Gist options
  • Save codebryo/10760185 to your computer and use it in GitHub Desktop.
Save codebryo/10760185 to your computer and use it in GitHub Desktop.
Quick way to replace mail-like addresses with real anchors.
/*
* mailConverter Script
* author: Roman Kuba, Codebryo
* web: codebryo.com
*
*/
// Lets pretend you want to have mail-address on your website but maybe want to protect it from being grabbed through bots
// This script turns a textelement into an useable link
// It removes spaces and replaces [at] with @ and [dot] with .
// Example:
// <span class="mailconv">mail [at] codebryo [dot] com</span>
// Result:
// <a href="mailto:mail@codebryo.com">mail@codebryo.com</a>
var myFunctions = {
mailConverter: function() {
var classname = 'mailconv',
targets = document.querySelectorAll('.' + classname),
eleng = targets.length;
for( var i = 0; i < eleng; i++) {
var node = targets[i],
text = node.innerText,
elem = document.createElement('a');
text = text.replace(/ /g, '');
text = text.replace('[at]','@');
text = text.replace('[dot]','.');
elem.setAttribute('href','mailto:'+text);
elem.innerText = text;
node.parentNode.replaceChild(elem, node);
}
}
};
myFunctions.mailConverter();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment