Potential Extensions

While this application is cool to play with as is, the next level is to send it in email. You can do that in three easy steps. First, copy the following function, and paste it between your SCRIPT tags:

function sendText(data) {
  paraWidth = 70;
  var iterate = parseInt(data.length / paraWidth);
  var border = '\n-------\n';
  var breakData = '';
  for (var i = 1; i <= iterate; i++) {
    breakData += data.substring((i - 1) * paraWidth, i * paraWidth) +
       '\r';
    }
  breakData += data.substring((i - 1) * paraWidth, data.length);
  document.CipherMail.Message.value = border + breakData + border;
  document.CipherMail.action =
     "mailto:someone@somewhere.com\?subject=The Secret Message";
  return true;
  }

This performs some last millisecond formatting before sending the email. The formatting inserts carriage returns every paraWidth characters. This ensures that the email message that the recipient receives isn’t one line of text 40 miles long. The next thing to do is add the second form required. Insert this code after the closing FORM tag in the current document:

FORM NAME="CipherMail" ACTION="" METHOD="POST" ENCTYPE="text/plain"
   onSubmit="return sendText(document.forms[0].Data.value);">
<INPUT TYPE=HIDDEN NAME="Message">
<INPUT TYPE=SUBMIT VALUE="   Send   ">
</FORM>

This form, named CipherMail , contains a lone HIDDEN field. The last thing to do is change the form references in the cipher algorithm functions.

Change lines 87-89:

var shiftIdx = (NN ? refSlide("caesar").document.forms[0].Shift.selectedIndex ...

Get JavaScript Application Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.