Created
September 13, 2011 14:22
-
-
Save joshbtn/1213916 to your computer and use it in GitHub Desktop.
Populate email fields in default email client - JavaScript.
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
// **prepareEmail(to, subject, body)** | |
// This comes in handy when attempting to obfuscate email address to prevent scrapers from grabbing them. | |
// This will take an email address, subject and a body and submit a form to the mailto: protocol, which should open up the default email client and populate the to, subject and body. | |
// For more information about what this does see http://support.microsoft.com/kb/279460. | |
// - *to* {String} - 'TO' e-mail address | |
// - *subject* {String} - subject line | |
// - *body* {String} - e-mail's body | |
function prepareEmail(to, subject, bodyText) { | |
var form = document.createElement('form'); | |
//Set the form attributes | |
form.setAttribute('method', 'post'); | |
form.setAttribute('enctype', 'text/plain'); | |
form.setAttribute('action', 'mailto:' + escape(to) + '?Subject=' + escape(subject) + '&Body=' + escape(bodyText ? bodyText : ' ') ); | |
form.setAttribute('style', 'display:none'); | |
//Append the form to the body | |
document.body.appendChild(form); | |
//Submit the form | |
form.submit(); | |
//Clean up | |
document.body.removeChild(form); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Really helpful thanks!