SMAM (short for Send Me A Mail) is a free (as in freedom) contact form embedding software.

form.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. function generateForm(id) {
  2. var el = document.getElementById(id);
  3. var input = {
  4. name: getField('form_name', 'Your name', false, 'input'), // TODO: configurable prefix
  5. addr: getField('form_addr', 'Your e-mail address', true, 'input'),
  6. subj: getField('form_subj', 'Your message\'s subject', false, 'input'),
  7. text: getField('form_text', 'Your message', false, 'textarea')
  8. };
  9. // Adding nodes to document
  10. el.appendChild(input.name);
  11. el.appendChild(input.addr);
  12. el.appendChild(input.subj);
  13. el.appendChild(input.text);
  14. // Adding submit button
  15. el.appendChild(getSubmitButton('form_subm', 'Send the mail'));
  16. }
  17. // Returns a form field
  18. // id: field HTML identifier
  19. // placeholder: placeholder text
  20. // email: boolean: is it an email field?
  21. // type: 'input' or 'textarea'
  22. // return: a div node containing a label and an input text field
  23. function getField(id, placeholder, email, type) {
  24. var field = document.createElement('div');
  25. field.setAttribute('id', id); // TODO: configurable prefix
  26. field.appendChild(getLabel(id, placeholder, type));
  27. field.appendChild(getInputField(id, placeholder, email, type));
  28. return field;
  29. }
  30. // Returns a label
  31. // id: field HTML identifier
  32. // content: label's inner content
  33. // type: 'input' or 'textarea'
  34. // return: a label node the field's description
  35. function getLabel(id, content, type) {
  36. var label = document.createElement('label');
  37. label.setAttribute('for', id + '_' + type);
  38. label.innerHTML = content;
  39. return label;
  40. }
  41. // Returns an input text field
  42. // id: field HTML identifier
  43. // placeholder: placeholder text, field description
  44. // email: boolean: is it an email field?
  45. // type: 'input' or 'textarea'
  46. // return: an input text or email field (depending on "email"'s value') with an
  47. // HTML id and a placeholder text
  48. function getInputField(id, placeholder, email, type) {
  49. var input = document.createElement(type);
  50. if(!type.localeCompare('input')) { // Set input type if input
  51. if(email) {
  52. input.setAttribute('type', 'mail');
  53. } else {
  54. input.setAttribute('type', 'text');
  55. }
  56. }
  57. input.setAttribute('required', 'required');
  58. input.setAttribute('placeholder', placeholder);
  59. input.setAttribute('id', id + '_' + type);
  60. return input;
  61. }
  62. // Returns a submit button
  63. // id: button HTML identifier
  64. // text: button text
  65. // return: a div node containing the button
  66. function getSubmitButton(id, text) {
  67. var submit = document.createElement('div');
  68. submit.setAttribute('id', id);
  69. var button = document.createElement('button');
  70. button.setAttribute('type', 'submit');
  71. button.setAttribute('id', id);
  72. // Disable button's default action
  73. button.setAttribute('onSubmit', 'return false;');
  74. button.innerHTML = text;
  75. submit.appendChild(button);
  76. return submit;
  77. }