Last active
August 29, 2015 13:59
-
-
Save codebryo/10760185 to your computer and use it in GitHub Desktop.
Quick way to replace mail-like addresses with real anchors.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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