Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Last active September 26, 2019 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WebReflection/dc9bf96e1e81e02131ce585a30a6e4cf to your computer and use it in GitHub Desktop.
Save WebReflection/dc9bf96e1e81e02131ce585a30a6e4cf to your computer and use it in GitHub Desktop.
// nullify the global reference while instrumenting the form
StaticEmail = (function (StaticEmail) {
contact.addEventListener('submit', function (event) {
event.preventDefault();
var from = contact.from.value.trim();
var md = contact.md.value.trim();
if (from && md) {
contact.submit.disabled = true;
StaticEmail({
path: '/api/paperboy',
subject: 'Contact from ' + from,
from: from,
md: md
})
.then(
thanks,
function (error) {
// this check is to make code testable via http-server
if (
error.message === 'Method Not Allowed'
// handle 405 errors as either OK (75%) or errors
// if you don't want errors, comment the following line
&& Math.random() < .75
)
thanks();
else {
alert(error);
contact.submit.disabled = false;
}
}
);
}
});
function thanks() {
var div = document.createElement('div');
div.textContent = 'Thank You!';
contact.parentNode.replaceChild(div, contact);
}
}(StaticEmail));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment