Skip to content

Instantly share code, notes, and snippets.

@luiskabes-arch
Last active July 27, 2023 12:36
Show Gist options
  • Save luiskabes-arch/186e2da8f2a23bd40d95d05a19e5a9c2 to your computer and use it in GitHub Desktop.
Save luiskabes-arch/186e2da8f2a23bd40d95d05a19e5a9c2 to your computer and use it in GitHub Desktop.
Encode an email on mailto: link
jQuery(document).ready(function ($) {
// Find all anchor tags with href starting with "mailto:"
$('a[href^="mailto:"]').each(function () {
var email = $(this).attr('href').substring(7); // Extract the email address
// Encode the email address for obfuscation
var encodedEmail = '';
for (var i = 0; i < email.length; i++) {
encodedEmail += '&#' + email.charCodeAt(i) + ';';
}
// Replace the href attribute with the encoded email
$(this).attr('href', 'mailto:' + encodedEmail);
// Add click event to decode the email and open default email client
$(this).click(function (e) {
e.preventDefault();
var decodedEmail = '';
// Decode the email address
var tempElement = document.createElement('div');
tempElement.innerHTML = encodedEmail;
decodedEmail = tempElement.innerText;
window.location.href = 'mailto:' + decodedEmail;
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment