Last active
July 27, 2023 12:36
-
-
Save luiskabes-arch/186e2da8f2a23bd40d95d05a19e5a9c2 to your computer and use it in GitHub Desktop.
Encode an email on mailto: link
This file contains hidden or 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
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