The Mailtool: Writing a Script-Based Tool - Doc JavaScript | WebReference

The Mailtool: Writing a Script-Based Tool - Doc JavaScript


The Mailtool

Writing a Script-Based Tool

The second mailto: URL tool we want to present here is based on a script only. It sets the message's details and then pops up a new mail message form, with all the details filled in. When you click the button below, it calls the function popupMessage(). Go ahead and click the button. See how the new mail message appears:

The button above is constructed as:


<FORM>
<INPUT TYPE="button" NAME="Test" Value="Test mailto:
  URL" onClick="popupMessage()">
</FORM>

The function popupMessage() is defined as follows:

function popupMessage() {
  // SET MESSAGE VALUES
  var to = "tomer@netscent.com";
  var cc = "yehuda@internet.com";
  var bcc = "tomer@internet.com";
  var subject = "Comments about your tips";
  var body =
    "Tomer and Yehuda,\n\n\tI'd like to suggest the following
      improvements to your tips: \n" +
    "\nFirst, ..." +
	"\nAlso, sometimes...." +
    "\n\nSincerely,\n\n" + "Your faithful reader...";
  // BUILD MAIL MESSAGE COMPONENTS
  var doc = "mailto:" + to +
    "?cc=" + cc +
    "&bcc=" + bcc +
    "&subject=" + escape(subject) +
    "&body=" + escape(body);
  // POP UP EMAIL MESSAGE WINDOW
  window.location = doc;
}

Notice the usage of the escape() function when assembling the subject and body fields. It converts any non-alphanumeric character to its ASCII value (in hexadecimal notation). In this way, any ampersands and question marks in the subject or body fields do not confuse the browser, when it interprets the mailto: URL. Question mark and ampersands are used as delimiters in the mailto: URL, and using them in the message items themselves without escaping, will surely mess up the interpretation of the mailto: URL. Popping up the new mail message form is done by assigning the mailto: text to window.location:

window.location = doc;

Next: The Tool's Code

http://www.internet.com


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: October 9, 2000
Revised: October 9, 2000

URL: http://www.webreference.com/js/column70/7.html